aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes STRANGE??????Final variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "STRANGE??????Final variables" Watch "STRANGE??????Final variables" New topic
Author

STRANGE??????Final variables

sunilkumar ssuparasmul
Ranch Hand

Joined: Dec 13, 2000
Posts: 142
Look at the following code . here i am able initialize the final variables twice how can this be happen? plz clarify
1)
class VariableTest {
public final int ii;
VariableTest()
{
ii=10;
}
VariableTest(int abc)
{
ii=100;
}
}

2)class VariableTest {
public final int ii;
protected static final int iii;
VariableTest()
{
ii=10;
iii=10;
}
}
when i initilaize iii i get the following two errors

Errror 1)variable 'iii' may not have been initialized. It must be assigned a value in an initializer, or in every constructor.
protected static final int iii;
^
Error 2) Can't assign a second value to a blank final variable: iii
iii=10;


But when i dont intialize iii I get the following error
Blank final variable 'iii' may not have been initialized. It must be assigned a value in an initializer, or in every constructor.
protected static final int iii;
WHY IS IT SO STRANGE . plz moderators and others throwe light
------------------
"Winners don't do different things
They do things differently"


"Winners don't do different things<br /> They do things differently"
Simon Sun
Greenhorn

Joined: Dec 07, 2000
Posts: 20
your first case is ok. I saw it in the book.
Your second case is the "protected" causing the problem.
Try to think protected means that classes in the same package and detrieved classes
can access that class. So when some classes in the same package access the class which
hasn't been initialized, so the blank final variable hasn't been initialized.
Am I say something wrong? I hope some expert can give us a claration as well.
simon
Aatif Kamal
Greenhorn

Joined: Jan 09, 2001
Posts: 2
Answer to you first problem is simple...
If you look at your code carefully you will find that actually you are not initalizing final variables twice. How is that... well you are intializing the the variable ii in two different construtors
1) VariableTest() --- 2) VariableTest(int abc)
Now when you create an instance of the class VariableTest you can use only one constructor at a time so the final variable will be initialzed only once either by constructor one or construtor two.For more clearfication check out the following code
public class test01
{
public static void main(String args[])
{
VariableTest vt1 = new VariableTest();
System.out.println(vt1.ii);
VariableTest vt2 = new VariableTest(10);
System.out.println(vt2.ii);
}
}
class VariableTest
{
public final int ii;
VariableTest()
{
ii=10;
}
VariableTest(int abc)
{
ii=100;
}
}
Answer to your second question is also very simple )
When u declare a variable static final then you must have to intialize it at the same position where it is declared.To rectify the problem use this code.
public static final int iii=10;
Thats why it was giving you the first error. Where as the second error is cuase of the fact that you cannot assign a value to a static final varibale in your code twice.

------------------
AK


<B><I>AK</B></I>
Ajay Patel
Ranch Hand

Joined: Jan 02, 2001
Posts: 39
hi Aatif & Simon,
i think that you guys are a bit confused about static final variables. Static final cannot be initialized in a constructor because a constructor is called on every object creation, but a static final var is to be initilized only once.
But this doesn't mean you have to initialize a static final at declaration. you can always use static initializer blocks.
The following code compiles perfectly:
class VariableTest {
public final int ii;
protected static final int iii;
static
{
iii=10;
}
VariableTest()
{
ii=10;
//iii=10;//error - cant initialize here.
}
}
-Aj
sunilkumar ssuparasmul
Ranch Hand

Joined: Dec 13, 2000
Posts: 142
Hi
Aj patel . as what have u said that static final variables are not initialized in constructor then why the compiler throes
such a error see bold letters.

Blank final variable 'iii' may not have been initialized. It must be assigned a value in an initializer, or in every constructor.

------------------
"Winners don't do different things
They do things differently"
Ajay Patel
Ranch Hand

Joined: Jan 02, 2001
Posts: 39
Hi Sunilkumar,
i compiler your code for q.2 and got only one compiler error:
VariableTest.java:7: cannot assign a value to final variable iii
iii=10;
^
1 error
please recheck.
i dont think that static final variables can be initialized in constructors.
-Aj
 
Don't get me started about those stupid light bulbs.
 
subject: STRANGE??????Final variables