Scientech Easy Object Declaration & Initialization in Java with examples | Scientech Easy

Monday, July 30, 2018

Object Declaration & Initialization in Java with examples

In this chapter, we will see 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 and after that, we will understand the concept of the Anonymous object.
Definition:

Declaration

The process of defining the variables along with its data type and name is called the declaration of the variables.
For example: In this example, we are declaring the name and city as variables with data type String. These variables are called instance variables.

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

Initialization

The process of assigning a value of the variable is called initialization. In other words, Initialization is the process of storing data into an object.
    class College { // Initialize the value of variables. String name="PIET"; String city="Nagpur"; }
Now, There are also three ways by which we can initialize the state of object i.e the value of variables in Java.
1. By constructor
2. By reference variable
3. By method.
Object declaration and Initialization in Java, Anonymous object in java
Object Initialization

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

By constructor

We will 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 default constructor. Student(){ // Step 3: Initialization of the values of object. name="Shubh"; rollNo=05; age=22; } // Step 4: Declare the method. void display(){ System.out.println("Student's name:" +name); System.out.println("Student's roll no: " +rollNo); System.out.println("Student's age:" +age); } // Step 5: Declare the main method. public static void main(String[] args) { // Step 6: Create the object of the class. Student st=new Student(); // It will call the default constructor. // Step 7: Now, call the display method using Static reference variable st because we cannot call non-static members directly in static region. st.display(); } }

    Output: Student's name: Shubh Student's roll no: 5 Student's age:22

By Reference variable

Let's understand this concept by taking a simple example where we will initialize the value of the object through reference variables.
Program source code 2:

    package scientech; public class Marks { // Declare the instance variables. String subject1; int sub1Marks; String subject2; int sub2Marks; // Declare the main method. public static void main(String[] args) { // Creates the object of the class. Marks mk=new Marks(); // Initialize the value of object through static reference variables mk.subject1="Science"; mk.sub1Marks=90; mk.subject2="Maths"; mk.sub2Marks=99; // Here, we added total marks with data type int. int totalMarks=90+99; // Now call marks using static reference variable. 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

By method

In this example, we are creating the two objects of Rectangle class and we will initialize the value to these objects by calling the perValue() method and display the state(data/value) of the objects by calling the calculate() method using the static reference variable because we cannot call directly non-static member into static region.
Program source code 3: Now follow all the above similar steps. 

    package rectangle; public class Rectangle { int length; int breadth; // Declare the method and initialize the parameters. 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) { // Creates the first object. 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. // Creates the second object. 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; } 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 the better approach than previous examples because in real time development we create several classes 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

An object which has no reference variable is called an anonymous object. Anonymous means nameless.  If you want to create only one object in a class then the anonymous object is a good approach.
Syntax to declare the anonymous object like this:
       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.
    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 the 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:
I hope that this article will help you to understand the concept of Object declaration and Initialization in Java with examples and program. The concept of the Anonymous object is also important. So practice its program. 

Next ➝ Life cycle of Object