Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem in constant and compile time constant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem in constant and compile time constant" Watch "Problem in constant and compile time constant" New topic
Author

Problem in constant and compile time constant

akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
Hi everybody

What is the differnce between the following statements :
final int a=1 ;
&
final int b;
b=2;

in context of the following code :

public class A
{ public static void main(String[] args)
{ final int a=1 ;
final int b;
b=2;
int x=0;
switch(x)
{case a :
case b :
}
}
}

Compiling this code gives the following error on the line "case b" :
constant expression required .

This example is from the kathy and berts book . It says that the case argument has to be a compile time constant . Please explain why "b" is not a compile time constant , despite being marked as final .

And a few additional doubts too.
> Since we have to initialize local variables when they are declared , why is initialization not needed in case of "b" . (Is it because of the final keyword ? )

> I further added the lines a++ and b++ . In the previous case the error was "cannot assign a value to the final variable a " , which is ok . But in the latter case the compiler returned "variable b might already have been intialized " . Why is the error different when "b" is also marked as final .
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83

Because the final int b is not assigned any value at compile time, in this code it will be bind to 2 at runtime therefore the compiler complains that he doesn't know the value of b at compile time. If you would assign value 2 to b in the same line as final int b =2 then the code will compile fine.

Note, local variables are required to be initialized only before they are used in a method not necessarily when they are declared.

Cheers!!!
akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
Thanks Mo ,

But why does the compiler shoot different error messages in case of a++ and b++ , when "a" and "b" both are marked as final ?

Regards
Aakash
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
With my compiler it is gererating the same error message for both a++ and b++.
The error is: A.java:8: cannot assign a value to final variable a
a++;
^
1 error


I don't know what you did to get the other error message for b++.

akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
Hey Mo
got the compile error wrong in my first post. sorry for that . instead of "initialized" it the error says "assigned" .
Here's the code and the error :

public class Z
{ public static void main(String args[])
{ final int a=1 ;
final int b;
b=2;
b++;
int x=0;
switch(x)
{case a :
//case b :
}
}
}

C:\java>javac Z.java
Z.java:6: variable b might already have been assigned
b++;
^

I dont know if its something in the code that i got wrong ? But doing a++ instead of b++ in the codethe error is concerning with the final modifier .

Regards Aakash.
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
I think may be it is the version of the JVM you are using is different from mine because somehow I cannot get the same error as you.

But from the error message I can say it is because b is declared final, so when you are tryimg b++ it means to the compiler that you are trying to assign another value(b=b+1) to a variable that cannot be modified because it is constant (final).

Cheers!!!
akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
Thanks again mate .
Please also see my doubt in the topic titled "please explain this Assertions problem" started by sonali Sehagl . This one really has me doubting my jvm and pullinging my hair. .
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
b cannot be used in a case statement because it is not a compile time constant. A code block or constructor could set it to any run time derived value.

As to you error message, I get Using Java 1.6

Now, this makes sense. "finals" do not have to be given a value when they are declared. Code blocks and constructors are allowed to set them too. But once set, that's it; they cannot change. At this line the compiler knows that another line of code has already assigned a value to b, so it throws this exception. If you comment out "b=2", the exception changes slightly but basically means the same thing, you can't use ++, -- etc with finals.

What does ++ mean? Increment by one. Increment what by one? The value of the primitive member. So the member must first be given a value. But it's final...you're not allowed to change it once it is set. At this point the compiler stamps its feet and goes off in a huff.


SCJP6
akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
one last thing . Why are the errors different in case of a++ and b++ . Is it a jvm a thing or is it because "a" has been assigned a value when it is declared. The error in case of a++ is :


C:\java>javac Z.java
Z.java:6: cannot assign a value to final variable a
a++;
^
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
I think that's it akaash. a was assigned at declaration, b wasn't.
The compiler knows for sure that a was assigned and where.
For b it's almost like the compiler knows it gave b a value, but is not quite sure where it did it.

I do not know why the message is "variable b might already have been assigned", to me it should read "variable b was assigned elsewhere".

Try commenting out "b=2;" and see what you get. That word "might" again!
akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
thanks mo and jason .
"strange are the ways of jvm " ..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in constant and compile time constant
 
Similar Threads
switch case with final
switch question
Swtich Statement
compile time constant
Complie time Constant - final int a; a =1 ; IS NOT?!