• 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

static final variables

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<CODE><PRE>class finals {
static final int MAX_SIZE; //Look at this it is static final
finals() {
MAX_SIZE=10;
System.out.println(MAX_SIZE);
}
finals(int a) {
MAX_SIZE=20;
System.out.println(MAX_SIZE);
}
void finals() {
//MAX_SIZE++;
System.out.println(MAX_SIZE);
}
public static void main(String[] args) {
finals f = new finals();
finals f1 = new finals(20);
f1.finals();
}
}</PRE></CODE>

When i use ststic final iam getting three compilation errors as:
finals:java:2: Blank final variable 'MAX_SIZE' may not have been initialized.It must be assign a value in an initializer,or in every constructor
static final int MAX_SIZE;
finals.java:4: Can't assign a second value to a blank final variable: MAX_SIZE
MAX_SIZE=10;
finals.java:8: Can't assign a second value to a blank final variable: MAX_SIZE
MAX_SIZE=20;
3 errors
Look at above code i have already initialized final variables in each and every constructror.

 
Trailboss
Posts: 23780
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just guessing here, but I think the problem is that it is static. What if you make two finals objects? The second constructor can't change the final value, right?
I think if it is static, you have to initialize it right then. If it isn't statice, you can initialize it in the constructors.
Just my guess.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think we can alternatively have a static initializer which assigns value to the static final variable. Looks like we can not assign value to a static blank final inside a constructor as Paul has mentioned.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Wheaton:
Just guessing here, but I think the problem is that it is static. What if you make two finals objects? The second constructor can't change the final value, right?
I think if it is static, you have to initialize it right then. If it isn't statice, you can initialize it in the constructors.
Just my guess.



Hi:
Based on my trial and error programs, I think if a variable is declared static final, it can either be initialized during the declaration itself OR it can be initialized in a static initializer block. It cannot be initialized in every constructor. But, I don't understand the error message in that case. "Blank final variable may not be initialized. It must be assigned a value in an initializer or in every constructor". Can someone explain this in this context, please?
Also, Paul's statement that "What if you make two finals objects? The second constructor cannot change the final value?" is not correct, I think. When you have the declaration as final <<type>> <<variable_name>> (i.e. static removed from the declaration), you can definitely initialize the variable in every constructor. It is perfectly valid. You can thus create two finals objects!
reply
    Bookmark Topic Watch Topic
  • New Topic