• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please explain final

 
Fox Hu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I use
public final int a;
a = 10;
there will be an error
But
public int a;
is OK
I think a variable can be declared without initialized in the class level.
Why is wrong with final ???
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
variables marked final can not be changed. Their value is final. In the first example, a is assigned the value of 0 and it can never be changed.
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Paul:
variables marked final can not be changed. Their value is final. In the first example, a is assigned the value of 0 and it can never be changed.

A small addition


Hope this helps
Sri
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And one last comment:
If you don't assign a value to your final variable (instance) then you can assigna value in your constructors. But, if you assign a value in one constructor then you MUST assign a value in all constructors:
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fascinating, especially the bit where it is okay if you implement an assignment to a final instance variable in all the constructors.
would it be okay to have a third constructor which said int a = 21; i wonder. probably.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just nitpicking Sri's example a bit...
Outside of any block, simple assignments can only take place when the identifier is declared. A simple assignment statement such as a = 2; is not allowed. It is possible to declare and initialize some variable on one line, then use it in a compound assignment statement later. Again, the assignment is somehow involved with a declaration. And for the curious, a couple of links to the JLS on final variables:
http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#10931
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#35962
[ January 23, 2003: Message edited by: Dirk Schreckmann ]
 
Fox Hu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
 
Joanne Fiorilli
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A variable that is final cannot be reassigned, a method that is final cannot be overidden.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what about a final variable in a method - it wouldn't be assigned a value unless the code in the declaration initialises it too?
[ January 23, 2003: Message edited by: Jasper Vader ]
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dirk,
The JLS links have been of great help
Sri
 
Fox Hu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>
what about a final variable in a method - it wouldn't be assigned a value unless the code in the declaration initialises it too?

I think in a method every variable must be assigned a value explicitly before it is used includes final.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ths,
the thing is, a class variable will be assigned (implicitly initialised) the value of zero if it is not explicitly initialised... so
final int a
will always be the value of zero (unless, as Thomas Paul mentioned, that final int a is initialised explicitly in every constructor for the class).
but a method variable is not implicitly initialised... so
final int a
in a method will not implicitly assign the value of zero to it.
This is the concept I am wondering about.
 
Fox Hu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right it is a very important concept.
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi tried executing the sri example
public class Clear
{
public final int a ;// this is where a gets intialized
//a = 10; //So this gives a compiler error
public static void main( String []args )
{
final int b;// as it is a local method, b does not get initialized here
b = 10;// as we are assigning b fro the first time, its ok
// b = 15; //compiler complains
}
}
Compiler did not compile untill i assigned the value for a. I am using jdk1.3 on windows 2000.
Any light why is this happeing.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun, simple assignment statements are not allowed outside of a block. Assignment statements combined with an identifier declaration are allowed outside of a block.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what about a final variable in a method - it wouldn't be assigned a value unless the code in the declaration initialises it too?
A final variable simply declared in a method would not be assigned a default value. Before using the variable, an assignment would be required. The assignment code could be within the same statement that declared the variable, or it could be an assignment statement later in the method. This is not unlike declaring and initializing a regular ol' non-final variable within a method.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i see, thanks for the clarification Dirk
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic