Results 1 to 11 of 11
  1. #1
    munishvit is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Lightbulb who initializes static members?

    Hi all,

    Instance variables of a class, if we don't write any constructor, they get automatically initialized to default values by default constructor. But for static variable, we don't have anything like automatic static constructor (even static constructors don't exist in Java) and if we don't write static initialization block, then how they get initialized to default values? :confused:

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by munishvit View Post
    Hi all,

    Instance variables of a class, if we don't write any constructor, they get automatically initialized to default values by default constructor. But for static variable, we don't have anything like automatic static constructor (even static constructors don't exist in Java) and if we don't write static initialization block, then how they get initialized to default values? :confused:
    The JVM does it for you; after the class (the methods etc.) is loaded, the variables are initialized; eithe explicitly because you defined initialization code or implicitly where all bits are set to zero.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    default constructor is static YES/NO? experts?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by Eranga View Post
    Define it as static and compile :D
    Do you even need to???

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    thought so lol :D

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Eranga do you celebrate christmas?

  10. #10
    munishvit is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks Josah...

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  2. Replies: 4
    Last Post: 05-12-2010, 11:05 AM
  3. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •