Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final variables 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 "final variables" Watch "final variables" New topic
Author

final variables

Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355



Compilation errors:


Why doesn't the it shows error for final variable declared inside Bar.java?
Please help!
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
It stopped after encountering the first error. Fix the first error and recompile it, then it will show the next error.
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Thanks!
Zhao zhenhua
Ranch Hand

Joined: Jul 17, 2006
Posts: 37



the public static final variable muse be initialized!
chang it to int BAR=5;
and cannot assign a value to the final varialbe.
[ May 27, 2007: Message edited by: Zhao zhenhua ]

Head first Servlet and JSP
Reet Goel
Greenhorn

Joined: May 02, 2007
Posts: 14
Hi there,

Preparing for SCJP 5.0

I came across this question:

Which line(s) should be removed for this code to compile?

static public void main(String...arg)
{
Byte snake = 5;
final int b;
b=2;
Integer i=5;
switch(snake){
case 128:System.out.println("Shouldn't it freeze in December ?");//1
default:System.out.println("White Christmas?");//2
case b:System.out.println("An Inconvenient truth...");//3
case i:break;//4
}
}
Answer:
1 is bigger than a byte
3 it must be a compile time constant. final int i=5; is one but final int i;i=5; is not a compile time constant.
4 you cannot convert an Integer to a Byte

I couldn't understand the explanation given for 3. Why is final int i;i=5; not a compile time constant?

Please help!

Thanks
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
JLS 3 - 4.12.4


15.28


Of course you don't need to memorize it all :-)
Just remember, that compile time constants (final variables) are initialized with value resolved at compile-time.

This can't be used in "case" statement:

[ June 07, 2007: Message edited by: John Stone ]
Uday Bhaskar Kopalle
Greenhorn

Joined: Feb 22, 2007
Posts: 6
Hi,

Notice the following about final variables:
1. All the member variables declared inside an interface are final by default and needs to be initialised at the time of declaration.

2. But incase of final variables declared in a class, they can be initialised at the time of declaration or inside the constructor of the class.

If you want to initialize inside the constructor of the class, in all the constructors of the class it(final variable) should be initialised. or otherwise you can also assign a parameter value of the constructor to the final variable as follows:

class FinalVariables {
final int a;
FinalVariables(int b)
{ a=b;}
}

This is one of the good features of java, with which we can assign a different value to the final variables for each object.
Reet Goel
Greenhorn

Joined: May 02, 2007
Posts: 14
Thanks to both of you,

good information you gave...I didn't know all that about final variables
 
GeeCON Prague 2014
 
subject: final variables