Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Where control goes  RSS feed

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a static variable , a static block & nain method in my class . Now my question is what is the control flow ( where control goes first on static variable or on static block or on main )

Thanks .
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are asking about a static initializer in a declaration like
"static int x = myMethod(3);"
The rule is the static initializers, both in declarations and in blocks, are executed in the order they appear in your program as part of the classloading process.

The main method is executed after the class containing main is loaded.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test {

static int x=10;
static {
int y=20;
}
public static void main(String s[]) {
int z=30;
}
}

Suppose this is the program then which variable ( x,y or z ) will initialized first .

& One more thing , what is the use of static block in a program .
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike answered that already.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer may be different !!!
like first x then y then z or something like that ...
 
Barry Higgins
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a class is called for the first time either by calling one of its static functions/members or by creating an instance of it all of its static initializers are called. This will include the static blocks of code. They will be called in the order that they are listed in the class. Next called is the method that is being called .. in this case the main.

Lastly you can check all this yourself by just trying to run the code before you post it!
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which variable ( x,y or z ) will initialized first .

x then y then z

what is the use of static block in a program

Sometimes initialization requires more than the simple assignment expression that will fit in a declaration.

"static int x = 4;" is one thing, but "static int squares[] = new int[50];" might require:
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike ,

Second answer was very clear .

but in first if suppose I declare static block first(y) then static variable(x) & then main(z) then sequence will be y,x & z .Right
( In any case all static variable initialized first , right )
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but in first if suppose I declare static block first(y) then static variable(x) & then main(z) then sequence will be y,x & z .Right

Yes
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!