• Post Reply Bookmark Topic Watch Topic
  • New Topic

final And static  RSS feed

 
Prakash Rai
Ranch Hand
Posts: 107
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Q1. what will be the output And why?
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you get when you tried to run this one? and what you expecting?
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So few rule reminders- QuoteYourSources

Did you try executing the code? The output would be clear then. And for why- You would have to tell us why
 
Prakash Rai
Ranch Hand
Posts: 107
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pramod P Deore wrote:What you get when you tried to run this one? and what you expecting?
"I am getting output :10 But here I am expecting static block to be executed . and whenever class will be loaded in main memory 1s t static block will be executed "
 
Callum Ludwig
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Simprakash

Static code blocks are run when you create an object of the class and you static block is part of the inner class Hello.You have not instantiated the Hello Class in the main method, so why should the static block for the inner class.

Adding the following line within the main method will out put:

10
10



If you try to add multiple instantiations:



You’ll find the output the same. Why because the static block only runs once before first instantiation of the class.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because Hello.a meets the definition of a compile-time constant expression, it can be evaluated entirely at compile time, and there is no need to load or initialize the class Hello. See also JLS 12.4.1: When Does Initialization Occur?.
 
Sean Keane
Ranch Hand
Posts: 590
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Because Hello.a meets the definition of a compile-time constant expression, it can be evaluated entirely at compile time, and there is no need to load or initialize the class Hello. See also JLS 12.4.1: When Does Initialization Occur?.


That is kinda interesting. How does Java access the static field if it does not load the class?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has all the information it needs at compile time, and it puts a copy of the actual value inside whatever other class needs it. So in the code above, the class Main is loaded and initialized, and it has its own copy of the number 10 that it uses when it gets to the code that says to print Hello.a.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Callum Ludwig wrote:Static code blocks are run when you create an object of the class

That's one of several possible events that would trigger execution of static initializers. A complete list is given in JLS 12.4.1 which I linked to above.

Since this is currently posted in Beginner, I would also note that beginning programmers don't really need to know or worry about this. For those of you who find it interesting and want to know more, that's great, but the topic should probably be moved so as not to scare off the other beginners.
 
Sean Keane
Ranch Hand
Posts: 590
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:It has all the information it needs at compile time, and it puts a copy of the actual value inside whatever other class needs it. So in the code above, the class Main is loaded and initialized, and it has its own copy of the number 10 that it uses when it gets to the code that says to print Hello.a.


Thanks Mike! That makes pretty logical sense
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote: . . . moved so as not to scare off the other beginners.
Who cares about the beginners? It scared me so it can be moved.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!