CodeGym /Courses /New Java Syntax /How to create objects

How to create objects

New Java Syntax
Level 10 , Lesson 1
Available

"OK. Last time we dealt with classes. Today, I'd like to tell you how to create objects. It's very easy. You write the keyword new and then the name of the class that you want to create an object of."

Example
Cat cat = new Cat();
Reader reader = new BufferedReader(new InputStreamReader(System.in));
InputStream is = new FileInputStream(path);

"I already know this."

"I know you do. Keep listening."

"When creating an object, you can pass various arguments inside parentheses. More about that later today. For now, let's take a look at the Cat class:"

Java code Description
class Cat {
    public String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
name is an instance variable. It has a public access modifier, making it visible anywhere in the project.

The getName method is a getter. It returns the value of the instance variable name. The method's name was derived from the word 'get' plus the variable's name with an uppercase first letter.

The setName method is a setter. It is used to assign a new value to instance variable name. The method's name was derived from the word 'set' plus the variable's name with an uppercase first letter. In this method, the parameter has the same name as the instance variable, so we precede the name of the instance variable with this.

"What are these getters and setters?"

"In Java, it's common practice to hide variables from other classes. Usually, variables declared inside classes have the private modifier."

"To allow other classes to change the value of these variables, a pair of methods is created for each of them: get and set. The get method returns the current value of the variable. The set method sets a new value for the variable."

"And what's the point?"

"If we don't want anyone to change the value of an instance variable, we can just not create a set method for it or we can make it private. We can also add additional data checks to the method. If the passed new value is invalid, nothing will be changed."

"I see."

"Because a class can have a lot of variables, the names of get and set methods usually include the names of the variable they deal with."

"If a variable is called 'name', then the methods will be called setName and getName, etc."

"I see. That seems quite reasonable."

"Here are more examples of working with a newly created object:"

Step Code Description
1
new Cat();
Create a Cat object
2
Cat catOscar = new Cat();
Store a Cat object in the variable catOscar
3
catOscar.name = "Oscar";
catOscar.age = 6;
catOscar.weight = 4;
Fill the object with data: name, age, weight
4
catOscar.sleep();
Call a method on the object
5
catOscar.fight(catSmudge);
Make the objects interact.

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

Comments (23)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Barkha Mishra Level 8, Delhi, India
26 August 2022
Don't forget to check the getters and setters link in the above article. It's extremely good!
Justin Smith Level 41, Greenfield, USA, United States
5 July 2021
#3 in the bottom table is kind of funny because they just explained that you shouldn't edit the variables directly like that, but rather through setter and getter methods.
Michael F Level 7, Albany, United States
3 July 2021
so what is the difference between 4 and 5? why is cat0scar.sleep ()blank and cat0scar.fight(catSmudge)
Justin Smith Level 41, Greenfield, USA, United States
5 July 2021
The sleep method doesn't need any other information The object affected by it (catOscar) goes to sleep. The fight method needs to know which cat is going to fight catOscar. So presumably the method declaration for fight lists a required argument.
Mihai Bone Level 8, Bucharest, Romania
6 October 2020
When I will get my first job in programming they will fire me in 2 weeks because I will call all my methods and variables cat names.
Dev Level 5, Abuja, Nigeria
19 August 2021
Lol! Codegym is full of cats... Cat class, objects etc
punkuotukas Level 7, Vilnius, Lithuania
18 September 2020
If I understood correctly what's indicated in the last table of examples, you can only assign certain variables to the object if you declare those variables in the object class, right? e.g.:

catOscar.name = "Oscar";
catOscar.age = 6;
catOscar.weight = 4;
I can only initialize name, age and weight if those variables are declared in the class Cat?
Steve Level 22, Carrollton, United States
27 September 2020
Correct. When you create catOscar, it inherits the attributes of the Cat object. So, the Cat object has to have name, age, and weight, and any cats created with that Cat object will also have name, age and weight.
Cristian Level 16, Bucharest, Romania
6 September 2019
Pertinent explanations!
Renat Mukhametshin Level 16, Pervouralsk, Russain Federation
1 April 2019
ok! good!
Mohit Swain Level 9, Bhubaneswar, India
19 December 2018
what would happen in getters if I write

public String getName() {
        return this.name;
    }
whats the difference returning this.name and returning name?
Ravi Kumar Level 5, Hyderabad, India
24 December 2018
this,name returns the instance variable value(Green) and name name returns the local variable(orange)
Mohit Swain Level 9, Bhubaneswar, India
28 December 2018
arent they both same here?
Syed Tayyab ul Mazhar Level 12, Karachi, China
6 January 2019
This :

public String getName() 
{
        return this.name;
}
and this :

public String getName()
{
        return name;
}
do the same thing. The this keyword doesn't have any effect in this context.
Mohit Swain Level 9, Bhubaneswar, India
8 January 2019
thanks for the explanation
Ashutosh Sharma Level 8, Delhi, India
14 March 2019
Wow!! SRK is at Level 8
P.B.Kalyan Krishna Level 22, Guntur, India
30 May 2019
this.name and name are same in this context.
Tayyab Mubeen Level 16, Lahore, Pakistan
23 September 2018
wow :D
Waleed Level 0, Pretoria, South Africa
28 August 2018
Are all lessons free throughout?
Atif Saeed Level 7, Islamabad, Pakistan
15 September 2018
yea man this course is free of cost
Ashutosh Sharma Level 8, Delhi, India
14 March 2019
Wow!! Islamabad. Is Islamabad beautiful??
Abdur Rehman Level 7, Faisalabad, Pakistan
14 May 2019
this course is in beta version once it goes full version it will not be free
27 December 2019
tera problem kya h beautiful ho ya na ho!