• 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

final variable question

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure that I understand everything about the initialization of the final variable. When can I initialize it in the constructor, and when not?
Thanks
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A final variable can be declared and have the initialization deferred until later. The acceptable places to do the deferred initialization are:
in initializer code
in a static initializer block
in a constructor if it is in EVERY constructor
If the final variable is also STATIC it must be initialized in a static initializer block.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy,
If variable is only final not static, can we initialize it in static initializer. Can we initialize any variable (either static or not) in static initializer or just static ones ?
 
Maja Grabovac
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Non-static variables cannot be accesed from the static code, therefore not from static initializer.
 
Ishaan Mohan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maja,
What about non static final variable ?
 
Maja Grabovac
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The same applies. You cannot access it from the static code because apart from being final it is also non-static.
 
Ishaan Mohan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That means Cindy is wrong ?
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well it means that I was not very clear.
The first set of places that I listed included all types of final variables. Then I mentioned the static ones separately. Obviously that could confuse a person.
final variables:
initializing code
constructors
static final variables:
static initializers
 
Ishaan Mohan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cindy.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a variable is static it must be assigned a value inside a class or static block. If the variable is final it can not be assigned a value inside a static block unless it is also static. I have some code below if you'd like to look at it.
public class Test{
final static int b;
static int c;
final int d;
{
//b = 100;
//c = 200;
d = 300;
}
static
{
b = 100;
c = 200;
//d = 300;
}
public Test(){}
public static void main(String[] args){
Test a = new Test();
//a.b = 100;
//a.c = 200;
//a.d = 300;
System.out.println("final static int b " + b);
System.out.println("static int c " + c);
System.out.println("final int a.d " + a.d);
}
}
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic