Initializing objects in JAVA

Initializing objects in JAVA

There are three ways to initialize objects in java

  • using reference variable

  • using methods

  • using constructors

1. Using reference variables

class Animal
{
    String color;
    int age;
    public static void main(String[] args)
    {
        Animal buzo = new Animal();
        buzo.color="black";
        buzo.age=10;

        System.out.println(buzo.color+" "+buzo.age);
    }
}

2. Using methods

class Animal
{
    String color;
    int age;
    void initObj(String c,int a)
    {
        color=c;
        age=a;
    }
    void display()
    {
         System.out.println(color+" "+age);
    }
    public static void main(String[] args)
    {
        Animal buzo = new Animal();
        buzo.initObj("black",20);
        buzo.display();
    }
}

3. Using Constructors

  • Constructor is a block similar to 'method' having the same name as that of the class name

  • It doesn't have any return type including void

  • Only a few modifiers apply to constructors. Those are public, protected, default, and private.

  • Constructors execute automatically when we create an object

class Test
{
    public Test()
    {


    }
    public static void main(String[] args)
    {
        Test t = new Test();
    }
}

Importance of constructors

class Employee
{
    String name;
    int emp_id;

    public static void main(String[] args)
    {
        Employee e1=new Employee();
        e1.name="sanjana";
        e1.emp_id=1716;
        System.out.println(e1.name+" "+e1.emp_id);
        Employee e2=new Employee();
        e2.name="sreeja";
        e2.emp_id=1717;
        System.out.println(e2.name+" "+e2.emp_id);
    }
}

In the above code using reference variables, the size of the code increases when we have to save the data of many employees. But by using constructors, the code can be simplified.

class Employee
{
    String name;
    int emp_id;
    Employee(String name,int emp_id)
    {
        this.name=name;
        this.emp_id=emp_id;
    }
    public static void main(String[] args)
    {
        Employee e1=new Employee("sanjana",1716);
        System.out.println(e1.name+" "+e1.emp_id);
        Employee e2=new Employee("sreeja",1717);
        System.out.println(e2.name+" "+e2.emp_id);
    }
}

Types of Constructors

In Java, constructors can be divided into 3 types:

  1. Default Constructor

  2. No-Arg Constructor

  3. Parameterized Constructor

1. Default Constructor

If we do not create any constructor, the Java compiler automatically creates a no-arg constructor during the execution of the program. This constructor is called the default constructor.

class Test
{
    int i;
    public static void main(String[] args)
    {
        Test t= new Test();
        System.out.println(t.i);
    }
}

Here, we haven't created any constructors. Hence, the Java compiler automatically creates the default constructor. The default constructor initializes any uninitialized instance variables with default values.

The above program is equivalent to:

class Test
{
    int i;
    Test() 
    {
        i = 0;
    }
    public static void main(String[] args)
    {
        Test t= new Test();
        System.out.println(t.i);
    }
}

2. No-arg Constructor

If a constructor does not accept any parameters, it is known as a "no-argument constructor".

class Test
{
    Test() 
    {
       System.out.println("no args constructor");
    }
    public static void main(String[] args)
    {
        Test t= new Test();
    }
}

3. Parameterized Constructor

A constructor accepting one or more parameters is called a parameterized constructor.

class Test
{
    Test(int a) 
    {
       System.out.println("no args constructor");
    }
    public static void main(String[] args)
    {
        Test t= new Test(10);
    }
}