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
2
Task
New Java Syntax, level 2, lesson 3
Locked
We solemnly publish and declare these variables...
Declare int variables called a and b. In the same line in which the variables are declared, immediately assign them different values. The values can be any integers.

"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
2
Task
New Java Syntax, level 2, lesson 3
Locked
A variable on our screen
Declare a String variable called name. In the line where we declare the variable, immediately assign it some value. Display the variable "name" on the screen.
undefined
2
Task
New Java Syntax, level 2, lesson 3
Locked
One variable is not enough
Declare int variables a and b and String variable s. In the same line in which the variables are declared, immediately assign them any different values.
undefined
2
Task
New Java Syntax, level 2, lesson 3
Locked
Display the square of a number
The variable number is given. Write a program that displays the square of this variable: (number * number).

A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.