wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assignment problem 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 "assignment problem" Watch "assignment problem" New topic
Author

assignment problem

Hritik Roy
Greenhorn

Joined: Aug 14, 2009
Posts: 18
final int a=1;
final int b ;
b=2;
int x=0;
switch (x) {
case a: //ok
case b: //compiler error
------------------------------------------------------
it is portion of a code,please don't be angry because i didn't write a complete program.
i don't want to ask why is the error..because i found at least 3 threads here dedicated on the run-time vs compile-time constants more or less same to this piece of code.

What confuses me is that why 'b' is run time constant. The same assignment, when i break it down in two lines, the nature changes.

can you please tell me the steps of memory allocation in both cases?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Hi Hritik, welcome to javaranch.

can you please tell me the steps of memory allocation in both cases?


It is not about memory allocation. b is not a compile time constant because the JLS says so. You can see here for details. Basically when you create a final variable and assign it a value right there, then the compiler can deduce the value at compile time and replace the value of the variable (or I should say constant) at every usage of the constant. So this code



becomes



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Hritik Roy
Greenhorn

Joined: Aug 14, 2009
Posts: 18
Thanks Ankit for your reply.As i have mentioned earlier,that i went through the other threads in this forum on this particular topic,eventually following the links, i went to JLS as well. However, Mr. Wong wonderfully summarized it in these few points-
1. declared as final
2. have a primative or String type
3. initialized (on the same line as the declaration)
4. assigned to a compile time constant expression

I understand that part,that is not my question. If i was passing the value through a method call to the variable, or if i used any expressions other than the ones mentioned in JLS,i would have agreed that it is run-time constant.

But my question is how " final int i=1" and "final int i; i=1;" these are different in terms of assigning the values? How come they are assigning the same value differently? i just want to know what happens when i compile and run these two lines,at what phase variables are initialized. Because unless that doubt is not cleared, i am failing to understand why the second expression does not qualify for the first bullet point of criteria of constant expression mentioned in JLS(Literals of primitive type and literals of type String )
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Expressions using for switch cases must be COMPILE TIME CONSTANTS.

COMPILE TIME CONSTANT is a literal or a final variable which is initialized during creation:
final int i = 10; //compile time constant
12 //compile time constant (integer literal)
"funny" // string literal (but it can not be used as case value)

This is NOT compile time constant:
final int i; //blank final
i=10; // compiler does not know the exact value

SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

3. initialized (on the same line as the declaration)


Hmmmm.... Maybe it should have been worded as "initialized during declaration". Which brings us to...

But my question is how " final int i=1" and "final int i; i=1;" these are different in terms of assigning the values? How come they are assigning the same value differently? i just want to know what happens when i compile and run these two lines,at what phase variables are initialized.


In the second case, the variable i is not initialized as part of the declaration -- and hence, isn't a compile time constant.

Now, you can argue that that the compiler should be smart enough to figure out the constant value, as the initialization is on the next line... but that is not how it is defined in the JLS. So, the variable i is not a compile time constant.

Because unless that doubt is not cleared, i am failing to understand why the second expression does not qualify for the first bullet point of criteria of constant expression mentioned in JLS(Literals of primitive type and literals of type String )


The value that is assigned to i (in both cases) *IS* a compile time constant. However, that doesn't mean that the variable i is a compile time constant -- as explained in the previous paragraph.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

BTW, here is the topic that hritik roy was refering to...

http://www.coderanch.com/t/454384/Beginning-Java/java/What-compile-time-constant

Henry
Hritik Roy
Greenhorn

Joined: Aug 14, 2009
Posts: 18
Now, you can argue that that the compiler should be smart enough to figure out the constant value, as the initialization is on the next line


Henry, exactly that was my problem, thanks for understanding it .Whenever i tried to think logically,i stumbled everytime.I should have wrote exactly these words of yours in my first post to make my question more clear.I apologize for that.

... but that is not how it is defined in the JLS. So, the variable i is not a compile time constant.


now that i know there is no particular reason behind it, its only because of what JLS says,so i will be ok with it, no more problems

thanks to all of you who replied.
 
Don't get me started about those stupid light bulbs.
 
subject: assignment problem
 
Similar Threads
switch case with final
final in switch case
switch question
switch case
Legal arguments in switch case statements