• 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 and static final

 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that final variable can be initialized latest in the constructors, but what about static final variables? Isn't it possible to initialize them in the static initializers?
TIA,
- Manish
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can init:
-final variables in constructors
-static final variables in static initializers
Otherwise you'll get compile-time error.
Sample:
======================================
class StatFinal{
static final int a;
final int b;
static {
a=1;//OK
b=1;//Compile-time error
}
StatFinal() {
a=2;// Will give compile error
b=2;// OK
}
public static void main(String args[]){
System.out.println(new StatFinal().a);
}
}
======================================
Jamal
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd just like to point out two quick things:

b=1;//Compile-time error


This is a compile error because you can't reference an instance variable in a static block; it's not really related to the final issue.


a=2;// Will give compile error


ALL final variables can only be initialized once, but you don't have to provide an initializer when you declare them. However, with a static final, by the time an instance method is invoked on the object (ie, an actual instance of the object is created via new) the static final variable will have been initialized with its default value (for an int, zero) by the classloader, so you cannot re-assign a different value later like in this method.
This implies that if you want your static final variables to have non-default values, you only have two choices:
1. Provide an initializer when you declare it (this is the common case)
2. Provide a static initilizer block that assigns the value. This is useful in the case where calculating the initial value is non-trivial, or dependant on the run-time environment.
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Ross:
I'd just like to point out two quick things:
ALL final variables can only be initialized once, but you don't have to provide an initializer when you declare them. However, with a static final, by the time an instance method is invoked on the object (ie, an actual instance of the object is created via new) the static final variable will have been initialized with its default value (for an int, zero) by the classloader, so you cannot re-assign a different value later like in this method.
This implies that if you want your static final variables to have non-default values, you only have two choices:
1. Provide an initializer when you declare it (this is the common case)
2. Provide a static initilizer block that assigns the value. This is useful in the case where calculating the initial value is non-trivial, or dependant on the run-time environment.



Thanks, thanks a lot Jamal and Rob
Approach 1 is indeed the best, but exams are most likely to test on 2.
- Manish
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic