Java Constructors with Examples

Java Constructors with Examples


Constructor In Java

  • Constructor is the special member of a class it is automatically invoked whenever an object of that class is created.
  • It has the same name as the class name in which it is declared it has no return type, not even void.
  • It is used to initialize the data members of a class.

Note -

  •  Explicitly constructor can not return any value, but implicitly it returns the current instance of a class.
  • A constructor can take parameters.
  • A class can have more than one constructor, but signatures are different.
  • Constructors follow the access rule of a class.
  • Constructor can not be inherited.
  • Constructor can not be final.
  • It is used to initialize the data members of a class.

The difference between constructor, and method -

  • Constructor is used to initialize the state of an object whereas method is used to expose the behavior of data member of a class.
  • Constructors have no return type not even void, whereas methods must have a return type.
  • It is invoked implicitly, the method is invoked explicitly.
  • Constructor name same as the class name, method name may, or may not same as the class name.
  • Java compiler automatically created default constructor if your class have no explicit constructor, No method definition is provided by compiler.

Example 1-

package javaLearnings;
import java.util.Scanner;
//Example 1
class Sample{
  
void show(){ // method
       System.out.println("show method here");
   }
}


public class Main {
  
public static void main(String[] args) {
 
// write your code here
       Sample ob = new Sample();
       ob.show();
      }

   }

Example 2 -

package javaLearnings;

//Example 2
class Sample{
  
void sample(){ // method
       System.out.println("Sample method here");
   }
}


public class Main {
  
public static void main(String[] args) {
 
// write your code here
       Sample ob = new Sample();
       ob.sample();
      }

   }

Example 3-

package javaLearnings;

//Example 3
class Sample{
    Sample(){
// Constructor
       System.out.println("Sample method here");
   }
}


public class Main {
  
public static void main(String[] args) {
 
// write your code here
       Sample ob = new Sample();
      }

   }

Example 4-

class Sample{
    Sample(){
// C.T. error
       System.out.println("Sample method here");
   }
}

 

Types of constructor

There are three types of constructor in java given as follows-

  1. Default constructor,
  2. Parametrized constructor
  3. Copy Constructor

Default Constructor

  • The constructor with no parameter is called default constructor.
  • If your class has no constructor then the compiler automatically created a default constructor in a class.
  • We need default constructor to initialize the value of data members with there default value

Example -

 

Output :

 Parameterized Constructor

A constructor with parameter is called parameterised constructor you must pass the value at the time of object creation if your class have parameterised constructor.

package javaLearnings;

class Sample<r> {
   String n;
  
int m,r;
    Sample(String a,
int b,int c){
  n =a;
  m = b;
  r = c;
    }
 
void show() {
     System.out.println(
"Name = "+n);
        System.out.println(
"Marks = "+m);
     System.out.println(
"roll No "+r);
}

}

public class Main {
  
public static void main(String[] args) {
 
// write your code here
       Sample ob = new Sample("Ram",90,400);
       ob.show();;
   }

   }

Output -