• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Mala Gupta book - confusion with static variable initialization

 
Greenhorn
Posts: 12
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi - In Mala Gupta's OCP Java SE7 book on page 110 it says "Because static variables can't be initialized using the constructors of a class, a static initializer block is used". I take this to mean that there will be a compiler error and not that its "good practice". So when I attempt to test this my code runs just fine when initializing a static variable in the constructor.

Can someone explain why this is working without error even though it says in the book (and elsewhere on the interwebs) that static variables cannot be initialized in a constructor?

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What value will x have if you don't create any instance of A?

Try declaring x as final (in addition to static) and see what happens.
 
Kirk Rohani
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:What value will x have if you don't create any instance of A?

Try declaring x as final (in addition to static) and see what happens.



Hi Pawel, thanks for the quick response. You can't initialize a static final variable in a constructor. I can make it simply final and that also compiles and works fine.

I'm just wondering why in this book and places like Stackoverflow it says a static variable cannot be initalized in a constructor, when clearly it works fine?
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because a static variable is not initialized in a constructor.
In your example it is initialized with a default value and then assigned a different value inside a constructor.
That is why I asked you what would be the value when no constructor is called.
And that is why I asked you about static final variable. A final variable must be initialized somewhere and you saw it can't be done in a constructor.
 
Kirk Rohani
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:Because a static variable is not initialized in a constructor.
In your example it is initialized with a default value and then assigned a different value inside a constructor.
That is why I asked you what would be the value when no constructor is called.
And that is why I asked you about static final variable. A final variable must be initialized somewhere and you saw it can't be done in a constructor.



Aha! Okay I see, yes that makes sense. I completely forgot that instance variables and class variables are initialized (in this case the int to a 0)!

Thank you so much for that!!
Kirk
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic