• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Basic Java

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a problem with static keyword
given a condition where
a)static block
b)static method
c)static variable
d)public static void main(String args[])
exists in a program.
What is the order of precedence of invocation ?
help
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not really an IO and Streams question, so I've moved it to Java in General (beginner) for you.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure what you mean by "invocation" in this context... I believe you mean "which exists first, and in what order do they get called"...

Basically all these things "exist" at the same time... when the JVM loads up the class, they are all created before you can access the class in any way.

The static block is executed whenever you first make any reference to the class it is in... create an object of the class? The static block is executed. Call a static method, or access a static variable? The static block is executed. Run a java class that has a static block? The static block is executed before main() begins.

However, in the static block, you can access static methods and variables, so you can technically think of them existing before the static block, but in reality, all these things "start existing" at the same time...

Technically, static methods ( Technically, the main() method falls into this category too... it is just a special case of a static method. ) are only invoked when you invoke them... the only exception to this is main()... the JVM knows that main() is the starting point of execution in a program...

Here is some code that may illustrate these concepts better than I have been at explaining them...








Try running StaticClass first, and then StaticClassTest and see the difference in output... hopefully this will help you see what is going on...

HTH,
-Nate
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic