Method name same as Class Name
void Sample3 is a method not a Constructor

package com.scjp.test;

public class Sample3 
{	
	void Sample3()
	{
		System.out.println("I am Constructor");
	}
	public static void main(String[] args) 
	{
		Sample3 objSample3 = new Sample3();
	}
}

OP:

Overloading Constructors

package com.scjp.test;

class Sample3
{
  public static void main(String args[])
  {
    Vasko objVasko1 = new Vasko(54);
    Vasko objVasko2 = new Vasko();
    System.out.println(objVasko1.Size);
    System.out.println(objVasko2.Size);
  }	
}

class Vasko
{
   int Size;

   Vasko()
   {	  
   }
	
   Vasko(int s)
   {
    Size = s;	  
   }
}

OP:

54
0

Leave a reply