Skip to content

How To Create An Object In Java

In this article, you will learn how to create an object in java along with accessing instance variables and methods of a class through created object. If you are beginners and want to start your career then join our java training institute and learn from beginning to advanced level to build your career in java development.

Let us first dive into the concepts of classes and objects.

Class- A class serves as a blueprint for creating objects. It contains properties(variables) and methods(functions) that determines the state or behaviour of an object.

A class is created by declaring the keyword ‘class’ followed by the ‘name of the class’. For example:
class Cuboid
{
// class body
}

The body of the class is contained between two curly braces.

Object- An Object may be defined as the sample of a class showing state or behaviour. In simple words, an object is created from a class.

CREATING A CLASS IN JAVA

A class in java can be created by using the keyword class followed by the name of the class. The members of the class(properties and methods) are declared under the curly braces as shown below in the example.

public class Cuboid {
public int length;
public int height;
public int width;
public int getVolume() {
return (length*height*width);
}
}

CREATING AN OBJECT IN JAVA

We have already described that an object is created from a class. The keyword ‘new’ is used to create an object in java programming language.

Following steps are involved in creating an object in Java

Step 1: Declaring a variable

To declare a variable in java, a modifier(public or private), field’s data type(int, string, boolean etc) and field name are required. For example;

public int length;
public int height;
public int width;

Step 2: Creating an object

The keyword ‘new’ is used as an operator to create the object. This operator also calls the object constructor.

If the constructor of the class is not written, the java compiler will automatically creates the constructor of that particular class.

The whole process is called instantiation which means creating an instance of a class.

Step 3: Initializing the object

The newly created object is initialized by making a call to the constructor by the new operator.

Example:

An example is shown below to create an object in java by following the above three steps.

public class Cuboid {
public int length = 15;
public int height = 5;
public int width = 10;
public int getVolume() {
return(length*width*height);
}
public static void main(String []args) {
Cuboid cuboidObj = new Cuboid();
System.out.println(“Volume of Cuboid is :” + cuboidObj.getVolume());
}
}

Output:

Volume of Cuboid is : 750

ACCESSING INSTANCE VARIABLES AND METHODS OF A CLASS IN JAVA

Member variables and methods can be accessed through created objects. Following are the steps to access member variables and methods.

Step 1: Create an Object

ObjectReference = new Constructor();

Step 2: Calling a Variable

ObjectReference.VariableName

Step 3: Calling a Method

ObjectReference.methodName(arg);

Example:

An example is shown below to access instance variables and methods of a class in java.

public class Cat {
public int catAge;

public Cat(String name) {
System.out.println(“Name of Cat is :” + name);
}

public void setAge(int age) {
catAge = age;
}

public int getAge( ) {
System.out.println(“Cat’s age is :” + catAge);
return catAge;
}

public static void main(String []args) {
Cat myCat = new Cat(“Daisy”);
myCat.setAge(4);
myCat.getAge( );
System.out.println(“Value of Instance Variable :” + myCat.catAge);
}
}

Output:

After running the above java codes, the following output will be generated.

Name of Cat is : Daisy
Cat’s age is : 4
Value of Instance Variable : 4

Facebook
Twitter
LinkedIn
Pinterest