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
Share on:

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

Leave a Comment