• 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 initialize 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 Userdefined Constructor(NoParametarized Constructor or Parametarized 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
import java.util.*;
class Factorial
{
	int n;
	Factorial()
	{
		Scanner s=new Scanner(System.in);
		System.out.println("Enter n value");
		n=s.nextInt();	
	}
	void fact()
	{
		int x,f=1;
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		System.out.println(f);
	}
}
class Main
{
public static void main(String args[])
{
	Factorial ff=new Factorial();
	ff.fact();
}
}

                                

Output:
Enter n value 5
120
2. No Parametarized Constructor with No return type with Parametarized Method

import java.util.*;
class Factorial
{
	int n;
	Factorial()
	{
		Scanner s=new Scanner(System.in);
		System.out.println("Enter n value");
		n=s.nextInt();	
	}
	void fact(int x,int f)
	{
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		System.out.println(f);
	}
}
class Main
{
public static void main(String args[])
{
	Factorial ff=new Factorial();
	ff.fact(1,1);
}
}                                

Output:
Enter n value 5
120
3. No Parametarized Constructor with return type with no Parametarized Method
import java.util.*;
class Factorial
{
	int n;
	Factorial()
	{
		Scanner s=new Scanner(System.in);
		System.out.println("Enter n value");
		n=s.nextInt();	
	}
	int fact()
	{
		int x,f=1;
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		return(f);
	}
}
class Main
{
public static void main(String args[])
{
	Factorial ff=new Factorial();
	int p=ff.fact();
	System.out.println(p);
}
}
                                

Output:
Enter n value 5
120
4. No Parametarized Constructor with return type with Parametarized Method
import java.util.*;
class Factorial
{
	int n;
	Factorial()
	{
		Scanner s=new Scanner(System.in);
		System.out.println("Enter n value");
		n=s.nextInt();	
	}
	int fact(int x,int f)
	{
		
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		return(f);
	}
}
class Main
{
public static void main(String args[])
{
	Factorial ff=new Factorial();
	int p=ff.fact(1,1);
	System.out.println(p);
}
}
                                

Output:
Enter n value 5
120
5. Parametarized Constructor with No return type with no Parametarized Method
import java.util.*;
class Factorial
{
	int n;
	Factorial(int m)
	{
		n=m;	
	}
	void fact()
	{
		int x,f=1;
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		System.out.println(f);
	}
}
class Main
{
public static void main(String args[])
{
	Scanner s=new Scanner(System.in);
	System.out.println("Enter n value");
	int n=s.nextInt();
	Factorial ff=new Factorial(n);
	ff.fact();
}
}
                                

Output:
Enter n value 5
120
6. Parametarized Constructor with No return type with Parametarized Method
import java.util.*;
class Factorial
{
	int n;
	Factorial(int m)
	{
		n=m;	
	}
	void fact(int x,int f)
	{
		
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		System.out.println(f);
	}
}
class Main
{
public static void main(String args[])
{
	Scanner s=new Scanner(System.in);
	System.out.println("Enter n value");
	int n=s.nextInt();
	Factorial ff=new Factorial(n);
	ff.fact(1,1);
}
}     
 

Output:
Enter n value 5
120
7. Parametarized Constructor with return type with no Parametarized Method
import java.util.*;
class Factorial
{
	int n;
	Factorial(int m)
	{
		n=m;	
	}
	int fact()
	{
		int x,f=1;
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		return(f);
	}
}
class Main
{
public static void main(String args[])
{
	Scanner s=new Scanner(System.in);
	System.out.println("Enter n value");
	int n=s.nextInt();
	Factorial ff=new Factorial(n);
	int p=ff.fact();
	System.out.println(p);
}
}

Output:
Enter n value 5
120
8. Parametarized Constructor with return type with Parametarized Method
import java.util.*;
class Factorial
{
	int n;
	Factorial(int m)
	{
		n=m;	
	}
	int fact(int x,int f)
	{
		
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		return(f);
	}
}
class Main
{
public static void main(String args[])
{
	Scanner s=new Scanner(System.in);
	System.out.println("Enter n value");
	int n=s.nextInt();
	Factorial ff=new Factorial(n);
	int p=ff.fact(1,1);
	System.out.println(p);
}
}

Output:
Enter n value 5
120