Lecture 8 - Cloning and References

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/3

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

4 Terms

1
New cards

What happens if a reference does not point to an object?

It will cause a NullPointerException, which is why we have to check null before accessing an object.

2
New cards

What happens if an object has no reference variable?

It is eligible for garbage collection - Java will reclaim the memory in heap.

3
New cards

If you want to clone objects, how would you do this?

Make an additional constructor:

public Cow(Cow other) { // this is a copy constructor

                                        this.name = other.name;

                    }

Then:

ArrayList<Cow> cows = new ArrayList<>();

                                        Cow cow1 = new Cow(“Alice”);

                                        cows.add(new Cow(cow1); // Stores a copy.

                                        cow1.name = “Bob”

System.out.println(cows.get(0).[MC1] name); //Output: Alice.

4
New cards

blah

blahh