• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Constructors In Java


Constructor
Constructor is a non-static method of a class.Which is used to initilize the values.
Syntax:

Classname(list of parameters / no parameters)
{
    ....
    ....
}

In the above syntax constructor name always class name.
In java language these constructers are classisfied into following types
1.Noparametarized Constructor
2.Parametarized Constructor
3.Default Constructor

1.NoParametarized Constructor:
If any Constructor signature does not contain parameters is known as NoParametarized Constructor
Syntax:

Classname()
{
    ....
    ....
}


2.Parametarized Constructor:

If any Constructor signature contains List of parameters is known as Parametarized Constructor
Syntax:

Classname(Datatype Variable1,Datatype Variable2, ....)
{
    ....
    ....
}


3.Default Constructor:
If any program does not contain any Usedefined Constructor the JVM creates one constructor is called Default Constructor.
Syntax:

Classname()
{
//Nobody
}



Based on the Constructors these methods are classified into following types
1.No Parametarized Constructor with No return type with no Parametarized Method
2.No Parametarized Constructor with No return type with Parametarized Method
3.No Parametarized Constructor with return type with no Parametarized Method
4.No Parametarized Constructor with return type with Parametarized Method
5.Parametarized Constructor with No return type with no Parametarized Method
6.Parametarized Constructor with No return type with Parametarized Method
7.Parametarized Constructor with return type with no Parametarized Method
8.Parametarized Constructor with return type with Parametarized Method

1. No Parametarized Constructor with No return type with no Parametarized Method
2. No Parametarized Constructor with No return type with Parametarized Method
3. No Parametarized Constructor with return type with no Parametarized Method
4. No Parametarized Constructor with return type with Parametarized Method
5. Parametarized Constructor with No return type with no Parametarized Method
6. Parametarized Constructor with No return type with Parametarized Method
7. Parametarized Constructor with return type with no Parametarized Method
8. Parametarized Constructor with return type with Parametarized Method

1. No Parametarized Constructor with No return type with no Parametarized Method

class MaxNum
{
int x,y;
MaxNum()
{
x=10;
y=20;
}
void max()
{
if(x>y)
{
System.out.println("X is Max Number");
}
else
{
System.out.println("Y is Max Number");
}
}
}
class Main
{
public static void main(String args[])
{
MaxNum m=new MaxNum();
m.max();
}
}


Output:
Y is Max Number
2. No Parametarized Constructor with No return type with Parametarized Method

class MaxNum
{
int x,y;
MaxNum()
{
x=10;
y=20;
}
void max(int a,int b)
{
if(x>y)
{
System.out.println("X is Max Number");
}
else
{
System.out.println("Y is Max Number");
}
}
}
class Main
{
public static void main(String args[])
{
MaxNum m=new MaxNum();
m.max(100,200);
}
}


Output:
Y is Max Number
3. No Parametarized Constructor with return type with no Parametarized Method

class MaxNum
{
int x,y;
MaxNum()
{
x=10;
y=20;
}
String max()
{
if(x>y)
{
return("X is Max Number");
}
else
{
return("Y is Max Number");
}
}
}
class Main
{
public static void main(String args[])
{
MaxNum m=new MaxNum();
String s=m.max();
SYstem.out.println(s);
}
}


Output:
Y is Max Number
4. No Parametarized Constructor with return type with Parametarized Method

class MaxNum
{
int x,y;
MaxNum()
{
x=10;
y=20;
}
String max(int a,int b)
{
if(x>y)
{
return("X is Max Number");
}
else
{
return("Y is Max Number");
}
}
}
class Main
{
public static void main(String args[])
{
MaxNum m=new MaxNum();
String s=m.max(100,200);
SYstem.out.println(s);
}
}


Output:
Y is Max Number
5. Parametarized Constructor with No return type with no Parametarized Method

class MaxNum
{
int x,y;
MaxNum(int p,int q)
{
x=10; // or x=p;
y=20; // or y=q;
}
void max()
{
if(x>y)
{
System.out.println("X is Max Number");
}
else
{
System.out.println("Y is Max Number");
}
}
}
class Main
{
public static void main(String args[])
{
MaxNum m=new MaxNum(1000,2000);
m.max();
}
}


Output:
Y is Max Number
6. Parametarized Constructor with No return type with Parametarized Method

class MaxNum
{
int x,y;
MaxNum(int p,int q)
{
x=10; // or x=p;
y=20; // or y=q;
}
void max(int a,int b)
{
if(x>y)
{
System.out.println("X is Max Number");
}
else
{
System.out.println("Y is Max Number");
}
}
}
class Main
{
public static void main(String args[])
{
MaxNum m=new MaxNum(1000,2000);
m.max(100,200);
}
}


Output:
Y is Max Number
7. Parametarized Constructor with return type with no Parametarized Method

class MaxNum
{
int x,y;
MaxNum(int p,int q)
{
x=10; // or x=p;
y=20; // or y=q;
}
String max()
{
if(x>y)
{
return("X is Max Number");
}
else
{
return("Y is Max Number");
}
}
}
class Main
{
public static void main(String args[])
{
MaxNum m=new MaxNum(1000,2000);
String s=m.max();
System.out.println(s);
}
}


Output:
Y is Max Number
8. Parametarized Constructor with return type with Parametarized Method

class MaxNum
{
int x,y;
MaxNum(int p,int q)
{
x=10; // or x=p;
y=20; // or y=q;
}
String max(int a,int b)
{
if(x>y)
{
return("X is Max Number");
}
else
{
return("Y is Max Number");
}
}
}
class Main
{
public static void main(String args[])
{
MaxNum m=new MaxNum(1000,2000);
String s=m.max(100,200);
System.out.println(s);
}
}


Output:
Y is Max Number