Constructors in Java

Discussion in 'Java' started by Sanskruti, Apr 25, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    Every class has at least one it's own constructor. Constructor creates a instance for the class. Constructor initiates something related to the class's methods. Constructor is the method which name is same to the class. But there are many difference between the method s and the Constructor.In this example we will see that how to implement the constructor feature in a class. This program is using two classes. First class is TestConstructor and second is the main class which name is TestConstructorClient.

    Code:
    Class TestConstructor{
      int x,y;
      TestConstructor (int a, int b){
        x = a;
        y = b;
      }
      TestConstructor (){
         x = 2;
         y = 3;
      }
      int area(){
        int ar = x*y;
        return(ar);
      }
    }
    public class TestConstructorClient {
       public static void main(String[] args)
       {
         TestConstructor a = new TestConstructor ();
         System.out.println("Area of rectangle : " + a.area());
         TestConstructor b = new TestConstructor (1,1);
         System.out.println("Area of rectangle : " + b.area());
       }
    }

    Constructor Overloading



    Constructors are used to assign initial values to instance variables of the class. A default constructor with no arguments will be called automatically by the Java Virtual Machine (JVM). Constructor is always called by new operator. Constructor are declared just like as we declare methods, except that the constructor don't have any return type. Constructor can be overloaded provided they should have different arguments because JVM differentiates constructors on the basis of arguments passed in the constructor.

    Whenever we assign the name of the method same as class name. Remember this method should not have any return type. This is called as constructor overloading. We have made one program on a constructor overloading, after going through it the concept of constructor overloading will get more clear. In the example below we have made three overloaded constructors each having different arguments types so that the JVM can differentiates between the various constructors.

    Code:
    public class ConstructorOverloading
    {
    	public static void main(String args[])
    	{
    		Rectangle rectangle1 = new Rectangle(2,4);
    		int areaInFirstConstructor= rectangle1.first();
    		System.out.println(" The area of a rectangle in first constructor is :  " + areaInFirstConstructor);
    		Rectangle rectangle2 = new Rectangle(5);
    		int areaInSecondConstructor= rectangle2.second();
    		System.out.println(" The area of a rectangle in first constructor is :  " + areaInSecondConstructor);
    		Rectangle rectangle3 = new Rectangle(2.0f);
    		float areaInThirdConstructor= rectangle3.third();
    		System.out.println(" The area of a rectangle in first constructor is :  " + areaInThirdConstructor);
    		Rectangle rectangle4 = new Rectangle(3.0f,2.0f);
    		float areaInFourthConstructor= rectangle4.fourth();
    		System.out.println(" The area of a rectangle in first constructor is :  " + areaInFourthConstructor);
    	}
    }
    
    class Rectangle
    {
      int l, b;
      float p, q;
      public Rectangle(int x, int y)
      {
        l = x;
        b = y;
      }
      public int first(){ 
        return(l * b);
      }
      public Rectangle(int x){
        l = x;
        b = x;
      }
      public int second(){
        return(l * b);
      }
      public Rectangle(float x){
        p = x;
        q = x;
      }
      public float third(){
        return(p * q);
      }
      public Rectangle(float x, float y){
        p = x;
        q = y;
      }
      public float fourth(){
        return(p * q);
      }
    }
     
    1 person likes this.
  2. mailtoabhishek88

    mailtoabhishek88 New Member

    Joined:
    Aug 3, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    why the default constructor is not provided by the compiler,if the class already have parameterized constructor ??
     
  3. geracool7

    geracool7 New Member

    Joined:
    Oct 3, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    rohtak
    hello i m sorabh doing btech final year and also doing java course from any private institute so i want to ask that how can i be perfect in java..any suggetion...
     
  4. Healthcare

    Healthcare Banned

    Joined:
    Jun 14, 2012
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Nice description. The example is the better method to understand the concept.
     
  5. tech nerd

    tech nerd New Member

    Joined:
    Oct 14, 2015
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    good article about constructors, It's very helpful and to the point. Most importantly it was short.
     
  6. sharon Maxwell

    sharon Maxwell New Member

    Joined:
    Feb 10, 2017
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Female
    Such a good coding about the constructor in JAVA ......but what I want to learn Advanced JAVA, plz suggest me. what are the best places? where could I learn Advanced JAVA.?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice