• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

final And static

 
Ranch Hand
Posts: 107
MyEclipse IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Q1. what will be the output And why?
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you get when you tried to run this one? and what you expecting?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 "
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4068
56
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?.
 
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4068
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4068
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 74712
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic