Creating objects - 1

"Hi, it's your favorite teacher again. Since you're making such great progress, I've decided to tell you about objects and how to work with them."

"To create an object, you need to type the keyword 'new' followed by its type name (class name). For example, suppose we have a class named 'Cat':"

Code Description
Cat cat;
Declares a Cat reference variable named cat. The variable cat's value is null.
new Cat();
Creates a Cat object.
Cat cat = new Cat();
Creates a Cat reference variable named cat.
Creates a new Cat object. Assigns a reference to the newly created object to the variable cat.
Cat kitty = new Cat();
Cat smokey = new Cat();
Two objects are created. References to them are assigned to two different variables.
Cat kitty = new Cat();
Cat smokey = new Cat();

smokey = kitty;
Two objects are created. References to them are assigned to two different variables.

Then we set the variable smokey equal to a reference to the object referenced by the variable kitty. Both variables now refer to the first created objects.
(Because the second object is no longer referenced anywhere, it is now considered garbage)

Cat kitty = new Cat();
Cat smokey = null;

smokey = kitty;

kitty = null;
One Cat object is created, and a reference to it is assigned to the first variable (kitty). The second variable (smokey) stores an empty (null) reference.

Both variables refer to the same object.

Now only smokey, but not kitty, refers to an object.

undefined
8
Task
New Java Syntax, level 8, lesson 3
Locked
Cats are good
Create a Cat object twice. Store each instance in its own variable. The variable names must be different.

"What would happen if we created an object and didn't save a reference in any variable?"

"If we just create an object without assigning it to a variable, the Java machine will create it and then declare it garbage (an unused object). After a while, the object will be disposed of during garbage collection."

"How do I dispose of an object I don't need anymore?"

"You don't. As soon as no variables refer to an object, it is labeled as garbage and destroyed by the Java machine the next time it collects garbage."

As long as there is at least one reference to an object, it's considered active and will not be destroyed. If you want to dispose of an object sooner, you can clear all references to it by assigning null to all variables that reference it.

"I see. Compared to the last few lessons, this looks pretty simple."

"Diego has been up all night thinking up tasks for you. He made this special effort just for you. He has a great sense of humor, you know?"

undefined
8
Task
New Java Syntax, level 8, lesson 3
Locked
Family relations
In the main method, create a Man object and save a reference to it in the variable man. Also, create a Woman object and save a reference to it in the variable woman. Hint: Use the following construct to create a Woman object and assign a reference to that object to the variable woman: VariableType v
undefined
8
Task
New Java Syntax, level 8, lesson 3
Locked
Three dogs are a force
Create 3 Dog objects. Store each instance in a separate variable. Give them the names "Max", "Bella", and "Jack".