What actually new keyword and Constructor does to a class?

Many Programmers will have doubts in the beginning of their career, what actually new keyword does and what exactly is the use of constructor, In worst cases some may also think constructor is to create an object which is actually not.

In simple definition new keyword is to create an object of a class and constructor is to initialize an object. so in runtime object is created first (new keyword is executed first) and then initialization of the object takes place (calling the constructor).

Lets do a simple POC on this concept.

First lets create a class StudentClass.java as  below

 package com.learning.newandconstructor;  
 public class StudentClass {  
      // Instance variables, scope of this variables can be accessed by all the  
      // methods and blocks of the class  
      String studentName;  
      int studentAge;  
      // parameterized constructor  
      public StudentClass(String name, int age) {  
           this.studentName = name;  
           this.studentAge = age;  
           // to support the statement Object is created first, If Object is  
           // created first then hashcode for the object also gets generated.  
           System.out.println(this.hashCode());  
      }  
      // no-arg Constructor  
      public StudentClass() {  
      }  
 }  

Lets create a test class to run the code.

 package com.learning.newandconstructor;  
 public class TestingthePoc {  
      public static void main(String[] args) {  
           StudentClass studentclassObject1 = new StudentClass();  
           /**  
            * from the above StudentClass studentclassObject1=new StudentClass();  
            * what happens is studentClass Object is created, Instance variables  
            * are set to its default values i.e, studentName value is set as null  
            * and studentAge is set to 0.  
            *   
            *  
            */  
           // values will be its default value  
           System.out.println("****************************************");  
           System.out.println(studentclassObject1.studentName);  
           System.out.println(studentclassObject1.studentAge);  
           System.out.println("****************************************");  
           StudentClass studentclassObject2 = new StudentClass("bharani", 26);  
           /**  
            * from the above StudentClass studentclassObject2=new  
            * StudentClass("bharani", 26); what happens is studentClass Object is  
            * created, Instance variables are set to its default values i.e,  
            * studentName value is set as null and studentAge is set to 0.  
            *   
            * since there is paramterized constructor call is happening here  
            * StudentClass("bharani", 26);  
            *   
            * we are passing the values through constructor, while intitialzing  
            * constructor sets the values to the instance variable  
            * this.studentName=name; this.studentAge = age;  
            *   
            * So now default values of the instance variables will be changed to  
            * the values which are passed through the constructor  
            *   
            */  
           System.out.println(studentclassObject2.studentName);  
           System.out.println(studentclassObject2.studentAge);  
           System.out.println("****************************************");  
      }  
 }  

Output:

 ****************************************  
 null  
 0  
 ****************************************  
 1534619972  
 bharani  
 26  
 ****************************************  

I am an open source enthusiast. Currently having 6 years of experience and part of Ericsson Global. I am a traveler, Coding manic, Foodie, Gaming Freak and an Amazing Cook.

Share this

Pages
Previous
Next Post »