CodeGym /Courses /New Java Syntax /Creating objects

Creating objects

New Java Syntax
Level 8 , Lesson 3
Available
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.

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?"

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
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".

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


Comments (147)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
abhishe_kira Level 18, India Expert
14 June 2023
I did a lot research to learn about object from youtube, but after this lesson it is the best that I understood that why we write : ClassName (instance variable) variable name = new ClassName( ) ; TY CodeGym.
Lishon Level 3, Nottingham , United Kingdom
7 July 2023
what is the "instance variable"? It's not the composite type is it?
abhishe_kira Level 18, India Expert
9 July 2023
instance of variable means a variable declared inside a class and so it will be available for every object of the class like int age; declared inside a human class will be available for a object named men and women both.
Lishon Level 3, Nottingham , United Kingdom
9 July 2023
Thank you.
Lishon Level 3, Nottingham , United Kingdom
10 July 2023
I did a bit more digging. Aren't objects and instances the same thing?
abhishe_kira Level 18, India Expert
15 July 2023
Yp they are but you know you can use instance word for variables too.
Fadhil Radhian Level 18, Semarang, Indonesia
12 March 2023
Amazing article as always !
Random Guy Level 9, India
18 February 2023
Ellie's lessons are easily the best and the easiest to understand, whoever is the writer for Ellie's lessons is amazing at teaching concepts.
Lunita Level 4, Dominican Republic
29 December 2022
I bookmarked this page, very important concepts.
Macailh Level 2, Mexico
17 December 2022
I help them translate into Spanish if they give me a free subscription What do you say captain Squirrels?
ValPereira Level 7, Minas Gerais, Brazil
26 October 2022
Gostaria de saber sobre tradução em portugues. grata
John Squirrels Level 41, San Francisco, Poland
27 October 2022
Currently, we have only 0-1 levels in Portuguese. Unfortunately, we can not provide any time frames for further translation.
zenjhiro nakata Level 2, Huancavelica , Peru
29 August 2022
con ayuda de los gatos aprendere a programar
zenjhiro nakata Level 2, Huancavelica , Peru
29 August 2022
algun dia sere un buen programador.
sari Level 2, Valencia, Spain
8 May 2022
para cuando la traduccion al español? :-(
John Squirrels Level 41, San Francisco, Poland
10 May 2022
Unfortunately, there are no time frames for the translation.
Amilkar Lopez Level 14, La Paz, Bolivia
13 April 2022
Super!