File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static final variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static final variables" Watch "static final variables" New topic
Author

static final variables

Roll
Ranch Hand

Joined: Jan 20, 2000
Posts: 52
<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.

paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20498
    ∞

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.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Thandapani Saravanan
Ranch Hand

Joined: Oct 17, 1999
Posts: 117
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.


Saravanan
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static final variables
 
Similar Threads
initializing a static final variable
final variable
Final - blank variables
Final Variables
assignment of final member variables