Java Program to Call a Method Using the Anonymous Object

Java Program to Call a Method Using the Anonymous Object

Java Program to Call a Method Using the Anonymous Object

// Java program to call a method using 
// an anonymous object

class Sample {
  void sayHello() {
    System.out.println("Hello World");
  }

  Sample() {
    System.out.println("Constructor called");
  }

}
public class Main {
  public static void main(String[] args) {
    new Sample().sayHello();
  }
}
Constructor called
Hello World

Hi, I'm Selva a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment