1/3
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
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.
What happens if an object has no reference variable?
It is eligible for garbage collection - Java will reclaim the memory in heap.
If you want to clone objects, how would you do this?
Make an additional constructor:
public Cow(Cow other) { // this is a copy constructor
}
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.
blah
blahh