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

static final variables..

edi kapllani
Greenhorn

Joined: Sep 14, 2001
Posts: 5

can you help me explaining why :
static final and final member variables need to be initialized explicitly at the declaration line (even not used in code);
thanks!
ciao!
------------------
edi


edi
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
declaring a variable as final means its value cannot be changed, and its value can only be set where it was declared.
eg.
final int myVar = 5;
// the following will not work
myVar = 6;
Also, declaring a class as final means it can't be extended (sub-classed).
Declaring a method as final means that method can't be overwritten (assuming the class that defines the method is not already final).
James.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Because it belongs to the class as a whole, and it can not be modified once a value is assigned. Since you can have a class without any objects being created, the only way the JVM can assure that static final values will have a value, is to make sure they are initialized at declaration time. final variables (non-static) are allowed to be initialized in the constructor(s).

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: static final variables..