Java default constructor

Java Programming Java8Object Oriented Programming

Yes! it is supported. A constructor with no arguments is called default constructor. It is used to initialize an object with default values. 

 example

Live Demo

public class Tester {
 
   private String message;
 
   public Tester(){
      message = "Hello World!";
   }
 
   public String getMessage(){
      return message ;
   }
 
   public void setMessage(String message){
      this.message = message;
   }
 
   public static void main(String[] args) {
      Tester tester = new Tester();
      System.out.println(tester.getMessage());  
   }
}   

Output

Hello World!
raja
Published on 22-Apr-2018 23:47:05
Advertisements