Does constructor return any value in Java?

Java 8Object Oriented ProgrammingProgramming

No, constructor does not return any value.

  • While declaring a constructor you will not have anything like return type.
  • In general, Constructor is implicitly called at the time of instantiation.
  • And it is not a method, its sole purpose is to initialize the instance variables.

Example

Live Demo

public class Sample{
   public Sample(){
      System.out.println("This is a constructor");
   }
   public static void main(String args[]){
      Sample obj = new Sample();
   }
}

Output

This is a constructor
raja
Published on 08-Jan-2018 06:32:22
Advertisements