Anonymous object in Java

Java Programming Java8Object Oriented Programming

Yes, we can use a method on an object without assigning it to any reference. 

 example

Live Demo

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

Here we've used message() method on new Tester() which is an anonymous object.

raja
Published on 22-Apr-2018 20:25:28
Advertisements