File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

static final variables..

edi kapllani

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);

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.
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).
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).


So much trouble in the world -- Bob Marley
I agree. Here's the link:
subject: static final variables..
It's not a secret anymore!