Day22 : Object Initialization

 Object Initialization

The word object initialization in Java means assigning initial value to instance variable at the time of creation of the object.
In Java we've three ways of doing this
  1. Using Explicit initialization
  2. Using Constructor
  3. Using Initializer block

1 - Using Explicit Initialization

        class < class-name>
        {
            <array> <data_type> <var> = value(This is an explicit initialization);
        }

Example :
File Name : Account.java

public class Account
{
private int accId = 101;
private String custName = "Amit";
private double balance = 50000.0;

public void showAccount()
{
System.out.println("accId "+accId);
System.out.println("Customer Name "+custName);
System.out.println("Balance "+balance);
}
}
File Name : UseAccount.java


public class UseAccount
{
public static void main(String[] args) {
Account obj;
obj = new Account();
obj.showAccount();
}
}

Output  : 
accId 101
Customer Name Amit
Balance 50000.0



2 - Using Constructor

In Java just like other object oriented language we've constructor. A constructor is a special method of a class having following important feature:
  1. They have some name as that of the class.
  2. They do not have any return type, not even void
  3. They are automatically called by java as soon as the object of the class gets created thus they are one of the best way to initialize as object.
  4. If we don't define any constructor in our class then java compiler automatically inserts a special constructor in our class called as default constructor. But since there are no executable statements in the body of default constructor. So as a programmer we never get to know that there is a default constructor present in the class.
  5. We must remember that the default constructor is only inserted by the compiler when we've not define any constructor ourselves. Otherwise the compiler withdraw the default constructor.
  6. If a programmer mention aby return type with constructor then although the java will not give any syntax error (Unlike C++) but java will not consider that method to be a constructor and will not call it on object creation.
  7. Unlike C++, In Java we don't have any copy constructor. So, If a programmer requires a copy constructor he will have to create it himself.

Initialization Using Constructor

File Name : Account.java

public class Account
{
private int accId = 101;
private String custName = "Amit";
private double balance = 50000.0;

// Creating Constructor followed by above rule
public Account(){
accId = 101;
custName = "Amit";
balance = 50000.0;
}

public void showAccount()
{
System.out.println("accId "+accId);
System.out.println("Customer Name "+custName);
System.out.println("Balance "+balance);
}
}

File Name : UseAccount.java

public class UseAccount
{
public static void main(String[] args) {
Account obj1;
obj1 = new Account();
obj1.showAccount();

Account obj2;
obj2 = new Account();
obj2.showAccount();
}
}

Output : 
accId 101
Customer Name Amit
Balance 50000.0
accId 101
Customer Name Amit
Balance 50000.0

Actually what happen in the above we're creating an constructor Account() and we just call them by driver class of different object will contain same value.

Creating Parameterized Constructor 

Just like we can have parameterized method in java similarly we also can have parameterized constructor i.e., constructor can accept argument But if we have only a parameterized constructor in the class then every object of the class which we will create must also be parameterized.

File name : Account.java

public class Account
{
private int accId = 101;
private String custName = "Amit";
private double balance = 50000.0;

// Creating Constructor followed by above rule
public Account(int ID, String name, double amt){
accId = ID;
custName = name;
balance = amt;
}

public void showAccount()
{
System.out.println("accId "+accId);
System.out.println("Customer Name "+custName);
System.out.println("Balance "+balance);
}
}

File Name : UseAccount.java

public class UseAccount
{
public static void main(String[] args) {
Account obj1;
obj1 = new Account(101,"Amit",50000.0);
obj1.showAccount();

Account obj2;
obj2 = new Account(102,"Deepak",60000.0);
obj2.showAccount();
}
}

Output : 
accId 101
Customer Name Amit
Balance 50000.0
accId 102
Customer Name Deepak
Balance 60000.0

Try Yourself : 
Problem : 
Modify the above code so that the driver class now access ID, name, and Balance from the user and then passes it to the constructor for initialization. Once all the object are initialized only then display their value. Do this for two object.


Comments

Popular posts from this blog

Day2 : Compiling Java Program with Examples

Day1: Introduction to Java and its feature

What does System.out.printf( "%-15s%03d\n", s1, x) do?