This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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

Statics doubt?

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please correct me if iam wrong....
All Static members i.e,methods & variables are compiled and run by the compiler itself as soon as the class is loaded by the compiler....
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rizwana mujawar wrote:Please correct me if iam wrong....
All Static members i.e,methods & variables are compiled and run by the compiler itself as soon as the class is loaded by the compiler....



Sorry, but no.

  • Variables aren't "run."
  • Nothing is run by the compiler. It's run by the JVM.
  • Methods are never run until we call them.


  • The keyword "static" simply means "associated with the class as a whole, rather than with individual instances."
     
    Ranch Hand
    Posts: 859
    IBM DB2 Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    When a Class is instantiated, all static initializers are executed first, then the constructor (if present).

    I like to ask this question in interviews! ;)

    WP
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    William P O'Sullivan wrote:When a Class is instantiated, all static initializers are executed first, then the constructor (if present).



    Not quite.

    Static initializers are run once, when the class is initialized (which is a separate step from when it's loaded, but for most purposes can be thought of as part of the loading process).

    Instance initializers are run once for each instance created, before that class's constructor (but after the parent's instance initializers and c'tor).

    And a constructor is always present.

    </pedant>
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic