• 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

Kathy / Bert static final variables

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
I was reading the book of Kathy / Bert and I got confused with something :
First: the book says that static member variables are automatically initialized with default values .
Second: the book says that all final member variables need to be initialized until the end of the constructor .
Question :
What happens if i have an static final member variable in a class ?
public class A
{
static final int b;
public static void main(String [] args)
{
System.out.println(b);
}
}
Will it be initialized ? Why ?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's another one you can compile youself.
Static variables are initialized to their default values when the class is loaded, constructors have nothing to do with it.
However, I believe that the compiler writers have made the assumption that if final is used in this context, the programmer probably intended that the variable should have a value different to its default. Perhaps a warning would have been better than an error, but it's better to be double sure in such cases. Maybe the Java Language Spec has something to say about it. (Take a look at section 13.4.8.)
(Marlene is just gonna love this one...)
[ March 31, 2003: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same q here. Got the result like this -
Like other final instance variables, it doesnt let u assign a value inside the constructor. If let without any assignment, it throws an error. Compiles and runs fine only if some value is given during declaration.
Kathy, help!
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A blank final variable must be initialized before use. So, use a static initializer:
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karthik, I was able to get the following code to run:

This, of course, would only work if your constant was a non-static instance variable.
[ March 31, 2003: Message edited by: Tom Purl ]
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For what I understood, here is the rule:
there are two ways for static final var:
1. static final int i = 10;
2. static final int i;
static { i = 10;}
for non-static final var:
1. final int j = 10;
2. final int j;
{ j = 10;}
3. initialize in constructor but not for static final var since it needs to be initialized before instance created.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS 8.3.1.2
See also JLS 8.3.2 and 4.5.5 (first bullet item)
[ March 31, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a quiz. Will they compile?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another quiz. While they compile?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic