File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final variables

 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Compilation errors:


Why doesn't the it shows error for final variable declared inside Bar.java?
Please help!
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 355
Chrome Java Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
Zhao zhenhua
Ranch Hand
Posts: 37
Firefox Browser IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
Reet Goel
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 332
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you,

good information you gave...I didn't know all that about final variables
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic