0

I tried to print something using static block without Main method. But how do I know at the time of the class loading static block will be called.

Here removing main method its not printing anything in CMD as well as in Eclipse IDE.

Output :(with main method)

Static Block Called........ i :6

public class StaticBlock
{
  static int i = 5;
  static 
  {
      System.out.println("Static Block Called........");
    i ++;  
  }
  public static void main(String args[])
  {
      System.out.println("i :"+i);
  }
}
3
  • if you remove the main method then it should print Static Block called and give exception main not found Commented May 30, 2013 at 9:03
  • 2
    How are you running the class without main method ? Commented May 30, 2013 at 9:05
  • I assume you delete your main from the StaticBlock class but you are placing it in some other class that you are executing otherwise you are not execute anything Commented May 30, 2013 at 9:10

2 Answers 2

4

This is actually a behavioral detail which has changed in Java 7.

Prior to Java 7, whatever class is passed to the JVM as the application entry point, that class is loaded, initialized, and then the main method is looked up. Even if there's no such method, the class initialization code will have run. That includes any static initializers.

As of Java 7, the class will be loaded, but will not be initialized prior to looking up the main method. The JVM will abort with an error if the method is not found, and initialization will never occur.

Class loading vs. initialization

For many purposes this is just a subtle difference, but you have actually hit one where it is crucial. As per Java Language / Java Virtual Machine specifications, there is a clear distinction between:

  1. class loading: this happens at any time, and for any class, the specific JVM implementation sees fit. It means loading the binary contents of the .class file, parsing them, verifying the bytecode, building up the constant pool, and so on;

  2. class initialization: this happens at a precisely specified point, which is when the class is referred to (explicitly or otherwise) for the first time during a JVM run. At this point all the class initializers run.

Sign up to request clarification or add additional context in comments.

2 Comments

can you please provide link to main method lookup in Java 7
@AlpeshGediya Tried to find a decent one, but the best I got was a discussion on CodeRanch. Sorry.
3

Your StaticBlock class will not be loaded unless it is not referred form somewhere. Having the main method causes your class to be loaded because jvm loads the class when you run it. As soon as you refer your StaticBlock class, anywhere in your project, be it the main method in the same class or the main method in a different class. That will cause the class to be loaded and as soon as class will be laoded, static block in that class will be executed.

By refer I mean either you create an instance of it or you use any public method or field of the class using hte class name i.e StaticBlock.filed or StaticBlock.method().

In short, a class static block is executed when the class gets loaded by a classloader.

8 Comments

What do you mean by 'refer your StaticBlock class' ? Because just a reference to the class won't load the class. The constructor of the class, or any static method of the class has to be called to load the class.
And if a class is requested to be run, but it lacks a main method? Will it not be loaded first?
@MarkoTopolnik Why wouldn't it be? I mentioned "Your StaticBlock class will not be loaded unless it is not referred form somewhere". And By refer I mean either you create an instance of it or you use any public method or field of the class using hte class name i.e StaticBlock.filed or StaticBlock.method().
So it will be loaded even if there's no main method? And then the static block will run anyway, isn't that correct?
@MarkoTopolnik It has to be referred to be loaded. Having a main method causes it to be loaded because we tell the jvm to run this class. When jvm has to run the class it will be loaded.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.