In the previous tutorial, you have learned to create an object in Java. Now, in this tutorial, we are going to learn the Object declaration and initialization in Java with examples. We will learn the different ways to initialize the value or data of the state of the objects inside the class. We will also learn the concept of the Anonymous object in Java.

Declaration of State of an Object in Java

The process of defining the variable along with its data type and name is called the declaration of the state of an object. In a simple way, it is also called a declaration of variable.
For example, If we declare the name and city as variables with data type String, these variables are called instance variables in Java.

    class College{ //Declaration of Instance variables. String name; String city; }

Initialization of State of an Object in Java

The process of assigning the value of the variable is called initialization of state of an object. In other words, Initialization is the process of storing data into an object. 
For example:
    class College { // Initialize the value of variables. String name="PIET"; String city="Nagpur"; }

Ways to Initialize State of Object

There are three ways by which we can initialize the state of an object i.e the value of variables in Java.
1. By using constructor
2. By using a reference variable
3. By using a method.
Object declaration in Java, Anonymous object in java

Let's see one by one with a simple example program step by step.

Object Initialization in Java by using Constructor

Let's take one simple example where we will store data into the object using a constructor.
Program source code 1:
    package scintecheasy; public class Student { // Step 1: Declaration of instance variables i.e state of the object. String name; int rollNo; int age; // Step 2: Declaration of a default constructor. The constructor name must be the same as the class name. Student(){ // Step 3: Initialization of the values of state of objects i.e values of the variables. name="Shubh"; rollNo=05; age=22; } // Step 4: Declare an instance method and print the values of instance variables. void display(){ System.out.println("Student's name:" +name); // Since this is instance area. That's why, we can directly call the instance variables. System.out.println("Student's roll no: " +rollNo); System.out.println("Student's age:" +age); } // Step 5: Declare the main method. It is a static method. So, it is a static area. public static void main(String[] args) { // Step 6: Create an object of the class. Student st=new Student(); // It will call the default constructor. // Step 7: Now, call the display method using an object reference variable st because we cannot call non-static members directly in the static region. st.display(); } }
    Output: Student's name: Shubh Student's roll no: 5 Student's age:22

Object Initialization in Java by using Reference variable

We can also initialize the value of the object through a reference variable. Let's see a simple program to understand the concept.
Program source code 2:
    package scientech; public class Marks { // Declare the instance variables. This is an instance area or region. String subject1; int sub1Marks; String subject2; int sub2Marks; // Declare the main method. This is a static region. public static void main(String[] args) { // Create an object of the class. Marks mk=new Marks(); // Initialize the value of the objects through object reference variables mk.subject1="Science"; mk.sub1Marks=90; mk.subject2="Maths"; mk.sub2Marks=99; // Here, we are adding total marks with data type int. int totalMarks=90+99; // Now call marks using object reference variable and print the values. System.out.println("Marks in Science:" +mk.sub1Marks); System.out.println("Marks in Maths:" +mk.sub2Marks); System.out.println("Total Marks: " +totalMarks); } }
    Output: Marks in Science: 90 Marks in Maths: 99 Total Marks: 189

Object Initialization in Java by using Method

We can initialize the value of an object by using a method. Let's take an example program. In this example program, we will create two objects of the Rectangle class and initialize the value to these objects by calling the perValue() method. 
We will display the state(data/value) of the objects by calling the calculate() method using the object reference variable because we cannot call directly the non-static member into the static region.
Program source code 3: 
    package rectangle; public class Rectangle { int length; int breadth; // Declare an instance method and initialize the parameters l and b with data type int. void perValue(int l, int b){ // Here we are setting the name of parameter different from the name of the variable because we are not using this reference. length=l; breadth=b; } void calculate(){ int perimeter=2*(length+breadth); System.out.println("Perimeter of the Rectangle:" +perimeter); } public static void main(String[] args) { // Create the first object of the class. Rectangle rt=new Rectangle(); rt.perValue(20,30); // It will call the perValue method and initialize the values. After that it will be automatically initialized to the instance variables. rt.calculate(); // it will call calculate() method to display the output. // Create the second object of the class. Rectangle rt2=new Rectangle(); rt2.perValue(50,50); rt2.calculate(); } }
    Output: Perimeter of the Rectangle:100 Perimeter of the Rectangle:200
Program source code 4: 
    package squareProgram; public class Square { int side; Square(){ side=45; // Initialization of value of the instance variable. } void calArea(){ int area=side*side; System.out.println("Area: " +area); } void calPerimeter(){ int perimeter=4*side; System.out.println("Perimeter: " +perimeter); } public static void main(String[] args) { Square sq=new Square(); sq.calArea(); sq.calPerimeter(); } }
    Output: Area: 2025 Perimeter: 180
In all the above examples, we declared the main() method inside the class. Now, let's see another example in which we will define the main() method outside the class. 
It is a better approach than previous examples because we create several classes in real time development and use it from another class. So let's take one more example and follow all the above steps.
Program source side 5: 
    package circleProgram; public class Circle { int radius; void area(int r){ radius=r; } void calArea(){ double area= 3.14*radius*radius; System.out.println(" Area of circle: " + area); } void calCircumference(){ double circum=2*3.14*radius; System.out.println("Circumference of circle: " +circum); } }
    package circleProgram; public class CircleTest { // Declaration of main method outside the class. public static void main(String[] args) { Circle cr=new Circle(); cr.area(30); cr.calArea(); cr.calCircumference(); } }
    Output: Area of circle: 2826.0 Circumference of circle: 188.4

Anonymous object in Java

An object which has no reference variable is called an anonymous object in Java. Anonymous means nameless.  If you want to create only one object in a class then the anonymous object is a good approach.

Declaration of Anonymous object in Java

The syntax to declare an anonymous object in Java is given below.
       new Class_name();
For example:
       new Student();
We can pass the parameter to the constructor like this:
       new Student("Shubh"); 
If you want to call a method through the anonymous object then you can create like this:
       new Student().display();
We can also pass the parameter to the calling method like this:
       new Student().display("Shubh", 25);
Let's implement this concept in a program.
Program source code 6:
    package anonymousObject; public class Multiplication { // Declare the instance variables int a; int b; int c; int d; // Declare the parameterize constructor and initialize the parameters. Multiplication(int p, int q){ a=p; b=q; int ab=a*b; System.out.println("Multiplication of a and b:" +ab); } // Declare an instance method and initialize parameters. void multiply(int x, int y ){ c=x; d=y; int cd=c*d; System.out.println("Multiplication of c and d:" +cd); } public static void main(String[] args) { // Create the anonymous object and pass the values to the constructor and calling method. new Multiplication(25,25).multiply(10, 20); // We can also pass the different value with the same anonymous object but we cannot create another new anonymous object. new Multiplication(20,20).multiply(30, 30); } }
    Output: Multiplication of a and b:625 Multiplication of c and d:200 Multiplication of a and b:400 Multiplication of c and d:900
Final words
Hope that this article has covered all the concepts related to the Object declaration and Initialization in Java with examples and program. The concept of the Anonymous object is a very important concept. We will learn more on the anonymous object in a further tutorial in advance. We hope that you will have enjoyed this article.
Thanks for reading!
Next ➤ Life cycle of an object

⏪ Prev Next ⏩