aspose file tools*
The moose likes Beginning Java and the fly likes Please explain final Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please explain final" Watch "Please explain final" New topic
Author

Please explain final

Fox Hu
Ranch Hand

Joined: Jan 23, 2003
Posts: 49
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

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
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

Joined: May 05, 2000
Posts: 13974
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

Joined: Jan 10, 2003
Posts: 284
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.


giddee up
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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 ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Fox Hu
Ranch Hand

Joined: Jan 23, 2003
Posts: 49
Thank you very much.
Joanne Fiorilli
Greenhorn

Joined: Jan 23, 2003
Posts: 5
A variable that is final cannot be reassigned, a method that is final cannot be overidden.
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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

Joined: Jan 08, 2003
Posts: 366
Thanks Dirk,
The JLS links have been of great help
Sri
Fox Hu
Ranch Hand

Joined: Jan 23, 2003
Posts: 49
>
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

Joined: Jan 10, 2003
Posts: 284
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

Joined: Jan 23, 2003
Posts: 49
You are right it is a very important concept.
Arun Boraiah
Ranch Hand

Joined: Nov 28, 2001
Posts: 233
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.


Sharing is learning
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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

Joined: Dec 10, 2001
Posts: 7023
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

Joined: Jan 10, 2003
Posts: 284
i see, thanks for the clarification Dirk
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please explain final