Constructer

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 onstructor 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
  2. No Parametarized Constructor with No return type with Parametarized
  3. No Parametarized Constructor with return type with no Parametarized
  4. No Parametarized Constructor with return type with Parametarized
  5. Parametarized Constructor with No return type with no Parametarized
  6. Parametarized Constructor with No return type with Parametarized
  7. Parametarized Constructor with return type with no Parametarized
  8. Parametarized Constructor with return type with Parametarized

1.No Constructor whith No return type with no Parametarized


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);
	}
}

public class Rain {

	public static void main(String[] args)  {
	
		Factorial f=new Factorial();
		f.fact();
	}
	
	}

2.No Constructor whith No return type with Parametarized


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 f)
	{
		int x;
		
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		System.out.println(f);
	}
}

public class Rain {

	public static void main(String[] args)  {
	
		Factorial f=new Factorial();
		f.fact(1);
	}
	
	}

3.No Constructor whith return type with no Parametarized


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);
	}
}

public class Rain {

	public static void main(String[] args)  {
	
		Factorial f=new Factorial();
		int a=f.fact();
		System.out.println(a);
	}
	
	}

4.No Constructor whith return type with Parametarized


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 f)
	{
		int x;
		
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		return (f);
	}
}

public class Rain {

	public static void main(String[] args)  {
	
		Factorial f=new Factorial();
		int a=f.fact(1);
		System.out.println(a);
	}
	
	}

5.Parametarized Constructor whith No return type with no Parametarized


import java.util.*;
class Factorial
{
	int n;
	Factorial(int p)
	{
		n=p;
	}


	void fact()
	{
		int x,f=1;
		
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		System.out.println(f);
	}
}

public class Rain {

	public static void main(String[] args)  {
		int n;
		Scanner s=new Scanner(System.in);
		System.out.println("Enter n Value");
		n=s.nextInt();
		Factorial f=new Factorial(n);
		f.fact();
		
	}
	
	}

6.Parametarized Constructor whith No return type with Parametarized


import java.util.*;
class Factorial
{
	int n;
	Factorial(int p)
	{
		n=p;
	}


	void fact(int f)
	{
		int x;
		
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		System.out.println(f);
	}
}

public class Rain {

	public static void main(String[] args)  {
		int n;
		Scanner s=new Scanner(System.in);
		System.out.println("Enter n Value");
		n=s.nextInt();
		Factorial f=new Factorial(n);
		f.fact(1);
		
	}
	
	}

7.Parametarized Constructor whith return type with no Parametarized


import java.util.*;
class Factorial
{
	int n;
	Factorial(int p)
	{
		n=p;
	}


	int fact()
	{
		int x,f=1;
		
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		return (f);
	}
}

public class Rain {

	public static void main(String[] args)  {
		int n;
		Scanner s=new Scanner(System.in);
		System.out.println("Enter n Value");
		n=s.nextInt();
		Factorial f=new Factorial(n);
		int a=f.fact();
		System.out.println(a);
		
	}
	
	}

8.Parametarized Constructor whith return type with Parametarized


import java.util.*;
class Factorial
{
	int n;
	Factorial(int p)
	{
		n=p;
	}


	int fact(int f)
	{
		int x;
		
		for(x=1;x<=n;x++)
		{
			f=f*x;
		}
		return (f);
	}
}

public class Rain {

	public static void main(String[] args)  {
		int n;
		Scanner s=new Scanner(System.in);
		System.out.println("Enter n Value");
		n=s.nextInt();
		Factorial f=new Factorial(n);
		int a=f.fact(1);
		System.out.println(a);
		
	}
	
	}