I'm having trouble understanding the following code's execution. I want to follow the Java program so that I can understand how everything works together. I step up breakpoints in Eclipse but it doesn't explain why. Here's the code:
public class Sequence {
public Sequence() {
System.out.print("c ");
}
{
System.out.print("y ");
}
public static void main(String[] args) {
new Sequence().go();
}
void go() {
System.out.print("g ");
}
static {
System.out.print("x ");
}
}
The output to the code is x y c g. Can someone explain why this is? I thought the program entry point was public static void main but it appears static executes first?
The static block is executed before the main starts, so x get printed.
Then we enter the main, and we call
new Sequence().go();
Which calls the Sequence constructor. As per the static block, before The Sequence constructor gets called (so before a new Sequence object gets initialized), the instance block (the one written within the braces) gets executed, so y gets printed.
Then the constructor call prints c.
In the end, the go() method gets called on the newly created object, so g gets printed.
So the full output will be
x y c g
The JLS is of help here, chapter 12 to be precise.
First the static block will run. This will happen when the class is loaded by the ClassLoader.
Then main will run, this is executed by the JVM to start the application.
Then your constructor is executed when you call new Sequence(). The compiler will yank your instance initialiser (the bit in curly brackets) into the top of the constructor (after the implicit call to to the superclass constructor). So the bit in curly brackets runs first then the code in the constructor.
Finally the method go() is called.
So the output of the code is x y c g
Java execute static block after class loading and before any method. Main method is entry point for any program but it is however a method then static class initialization.
In your class you have used
//constructor
public Sequence() {
System.out.print("c ");
}
//instance init block
{
System.out.print("y ");
}
//method
void go() {
System.out.print("g ");
}
//static init block
static {
System.out.print("x ");
}
-> Init blocks execute in the order they appear.
->Static init blocks run once, when the class is first loaded.
-> Instance init blocks run every time a class instance is created.
-> Instance init blocks run after the constructor's call to super().
->Constructor run after when you create an instance.
According all of that rules you got,as expected x y c g output
First jvm loads static blocks when application starts. So static block executed first.
Then main method execution starts.
{
System.out.print("y ");
}
is the constructor block it will be copied to each constructor so when u instantiate class it will be called every time.
Click here
Steps:
i. When class is loaded then static block is executed first.
ii. Every time object of that class is instantiated then initialization block
i.e.
{
System.out.print("y ");
}
is executed(every time) and after that the time of constructor comes to be executed.
iii. When object creation is finished then go() method is executed.
And hence the output.
Related
class Welcome{
public static void main(String args[]){
System.out.println(Hello.a);
}
}
class Hello{
static int a=10;
static Hello h=new Hello();
{
System.out.println("IB");
}
static{
System.out.println("SB");
}
}
Output:
IB
SB
10
I really don't understand the logic behind this execution. According to me, the static variables are initialized and static block gets executed. Then, the instance variable is initialized and instance block is executed.
However, this seems to be a bit confusing. It would be a great help if a step-by-step order of actual execution is given.
What is the meaning of static Hello h=new Hello();?
How will this be treated?
I fixed the class keyword and ran it through. The execution is quite straight forward when you see it running.
Static variables are initialised. That means a and h are set.
Initialiser blocks are ran. So "IB" is printed. They run because the value of h is a new Hello object.
NOTE: This only happens because h is defined before the static initializer block in the class. If you change the order they appear in the code, then the order of execution will change.
Static initialiser blocks are ran. So "SB" is printed.
main method is invoked, which prints out Hello.a. So 10 is printed.
IDEOne Link: http://ideone.com/KfdS6n
When I try running this piece of code, it prints "x y c g ". I know that x has priority over y because it is static and the constructor (c) prints before the method (g), but what exactly is the purpose of putting two lines in their own blocks, and what does this help achieve?
public class Sequence {
Sequence() { System.out.print("c "); }
{ System.out.print("y ");}
public static void main(String[] args) {
new Sequence().go();
}
void go() { System.out.print("g "); }
static { System.out.print("x "); }
}
Your question is closely related to this one, but not exactly a duplicate: Why java Instance initializers?
When you first reference a class, it gets loaded and the static components get initialized in the static initializer. These blocks can be very useful, for setting up collections or loading native code.
An instance initializer is a bit of code that works similar to the static initializer except it operates on every instance before the constructor is invoked. It appears that there is no absolute need for this in Java since it can just be placed in the beginning of your constructor.
The output of following program is:
superstatic block
static blockn0
inmain
super constructor
constructor
Here is the code:
class StaticSuper{
static {
System.out.println("superstatic block");
}
StaticSuper(){
System.out.println("super constructor");
}
}
public class StaticClassExample extends StaticSuper{
static int rand;
static {
rand =(int) (Math.random() * 6);
System.out.println("static blockn" + rand);
}
StaticClassExample(){
System.out.println("constructor");
}
public static void main(String[] args) {
System.out.println("inmain");
StaticClassExample st= new StaticClassExample();
}
}
Why "inmain" is printed third though it is first in the main()? Please explain me the meaning of:
static {
// some lines
}
in the class.
The static block you're seeing is what we call a static initializer and its job is usually to initialize something in the class (perhaps a class-wide state or some kind of resource).
It is invoked when the class is loaded by the runtime and that's why it may be run before main is run.
1 : when you create an instance by using new == > it calls the constructor
2 : just when you constructor is called , the parent constructor is called
3 : and also whenever a class is loaded , even for a constructor , the static blocks are executed first ,
thus you get the order you are getting
since you are executing you class , the complete files is checked first for any static block , and static blocks are executed even before the main method gets called
Static blocks are called at the time classes are loaded. Thats why print outs of static are printed first.
Static variable means that there won't be separate copies of that variable for different instances of that class.
Static block of code is simply if you want to execute something at the time of class loading.
Please explain me the meaning of static { // some lines } in the
class.
It's called a static initializer. It's executed when the class is loaded (or initialized, to be exact). The static blocks will be executed before your main()method.
Why "inmain" is printed third though it is first in the main()?
Since static blocks are executed before your main(), it appears third in the print list and when you create the instance of StaticClassExample class, first its parent constructor is called and then its own constructor is called.
Hence, the output.
I'm looking at the code below and found something a bit strange:
public class Sequence {
Sequence() {
System.out.print("c ");
}
{
System.out.print("y ");
}
public static void main(String[] args) {
new Sequence().go();
}
void go() {
System.out.print("g ");
}
static {
System.out.print("x ");
}
}
I would've expected this to give a compilation error as the System.out with "y " doesn't belong to a method declaration just a { }. Why is this valid? I don't see how this code would or should be called.
When running this it produces x y c g also, why does the static { } get called before the sequence constructor?
This:
static {
System.out.print("x ");
}
is a static initialization block, and is called when the class is loaded. You can have as many of them in your class as you want, and they will be executed in order of their appearance (from top to bottom).
This:
{
System.out.print("y ");
}
is an initialization block, and the code is copied into the beginning of each constructor of the class. So if you have many constructors of your class, and they all need to do something common at their beginning, you only need to write the code once and put it in an initialization block like this.
Hence your output makes perfect sense.
As Stanley commented below, see the section in the Oracle tutorial describing initializaiton blocks for more information.
Its not a method but a initialization block.
{
System.out.print("y ");
}
It will be executed before the constructor call.
While
static {
System.out.print("x ");
}
is static initialization block which is executed when class is loaded by class loader.
So when you run your code
Class is loaded by class loader so static initialization block is executed
Output: x is printed
Object is created so initialization block is executed and then constuctor is called
Output: y is printed followed by c
Main method is invoked which in turn invokes go method
Output: g is printed
Final output: x y c g
This might help http://blog.sanaulla.info/2008/06/30/initialization-blocks-in-java/
That's an instance initialization block followed by a static initialization block.
{
System.out.print("y ");
}
gets called when you create an instance of the class.
static {
System.out.print("x ");
}
gets called when the class is loaded by the class loader. So when you do
new Sequence().go();
the class gets loaded, so it executes static {}, then it executes the instance initialization block {}, afterwards the body of the constructor is called, and then the method on the newly created instance. Ergo the output x y c g.
static {
System.out.print("x ");
}
Is a static block and is called during Class Loading
{
System.out.print("y ");
}
Is an initialization block
You can have multiple initialization blocks in a class in which case they execute in the sequence in which they appear in the class.
Note that any initialization block present in the class is executed before the constructor.
static {
System.out.print("x ");
}
is an initialization block shared by the class(as indicated by static), which is executed first.
{
System.out.print("y ");
}
is an initialization block shared by all objects(constructors) of the class, which comes next.
Sequence() {
System.out.print("c ");
}
is a particular constructor for the class, which is executed third. The instance initialization block is invoked first every time the constructor is executed. That's why "y" comes just before "c".
void go() {
System.out.print("g ");
}
is just an instance method associated with objects constructed using the constructor above, which comes last.
{
System.out.print("y ");
}
These kinds of blocks are called initializer block. It is executed every time you create an instance of a class.
At compile time, this code is moved into every constructor of your class.
Where as in case of static initializer block: -
static {
System.out.println("x ");
}
it is executed once when the class is loaded.
We generally use static initializer block when the initialization of a static field, require multiple steps.
It is used as an initialisation block and runs after any static declaration. It could be used to ensure that no one else can create an instance of the class (In the same way you would use a private constructor) as with the Singleton design pattern.
static {
System.out.print("x ");
}
Static blocks are only executed once when the class is loaded and initialized by the JRE.
And non-static block will be call every time your are creating a new instance and it will be call just before the Constructor.
As here you've created only 1 instance of Sequence so constructed has been called after non-static blocks and then the method which actually your goal.
Consider this code:
public class Main {
static String s = "-";
public static void main (String [] args){
go();
System.out.println(s);
Main m = new Main();
}
{go();}
static {go();}
static void go(){s+="s";}
}
Its output is:
-ss
the instance init block is never called, why?
It is called - AFTER you've printed s. Instance initializers are called when instances are created.
It is called. However it is called after the call to println because you create the first instance of Main of that. If you move the call to println to the end of main, you'll see three s.
Instance initialization block code runs right after the call to super() in a constructor, in other words, after all super constructors have run.
The order in which initialization blocks appear in a class matters. If a class has more than one they all run in the order they appear in the class file.
Some rules to remember:
List item Initialization blocks execute in the order they appear.
Static Initialization blocks run once when the class is first
loaded.
Instance Initialization blocks run every time a class
instance is created.
Instance Initialization blocks run after the
constructor’s call to super().
It is called, but after you print to the console. You aren't making an instance of it until after printing.
As the others have pointed out, the instance init block is called, but it doesnt affect the output of your System.out.println statement, because it is called in conjunction with the invocation of the instance of your class: Main m = new Main();
One thing you could do when trying to debug these cases is to dump a thread stack at the invocation point:
static void go() {
new Exception().printStackTrace(System.out);
s += "s";
}
This will allow you to see all the times that the go method is called, and by using the same print stream as your println in your main method, you can see the stacks in relation to the output of your s var.
In my version, the result looks like this:
java.lang.Exception
at Main.go(Main.java:29)
at Main.<clinit>(Main.java:25)
java.lang.Exception
at Main.go(Main.java:29)
at Main.main(Main.java:14)
-ss
java.lang.Exception
at Main.go(Main.java:29)
at Main.<init>(Main.java:21)
at Main.main(Main.java:17)