Java Program to Call Static Block Using the Anonymous Object

Java program to call static block using the anonymous object

// Java program to call static block 
// using an anonymous object

class Sample {
  static {
    System.out.println("Static block called");
  }

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

}

public class Main {
  public static void main(String[] args) {
    new Sample();
  }
}

Output

Static block called
Constructor called
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