File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final static variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final static variable" Watch "final static variable" New topic
Author

final static variable

Amit Sharma
Ranch Hand

Joined: Nov 22, 2005
Posts: 87
class Test {
static final int i;

Test() {i=1;}


public static void main(String args[]){Test t =new Test();}

}

////// compiler says cant assign a value to a final variable ////// but K&B book says final variable should initialize before the constructor completes. plz tell me what i am missing.
___________________________________________________________________________


Amit Sharma SCJP 1.4, SCWCD 5, OCUP-100(UML)
Gyanesh Sharma
Ranch Hand

Joined: Nov 27, 2005
Posts: 47
The most obvious problem is you are trying to use a static variable in a non static constructor.
If you make the member variable non static, you should not get that error.
Gyanesh Sharma
Ranch Hand

Joined: Nov 27, 2005
Posts: 47
I think I should add more to it.
The constructor would be executed every time an object is created. So the compiler doesn't like to allow setting the variable every time.
Amit Sharma
Ranch Hand

Joined: Nov 22, 2005
Posts: 87
static is not applied to constructors . and if we can initialize a nonstatic final variable frm constructor why we cant initialize a static final variable frm constructor
Purujit Saha
Ranch Hand

Joined: Nov 01, 2005
Posts: 86
Instance Final Variables ( Ex: final int a
-----------------------
Instance final variables should be initialized along with their declaration..or

If they arent ...initialized..

then you can initialize...the variable either in the constructor or initializer block.. else compilation error



Final Class Variables with static modifier (Ex:static final int b
------------------------------------------

Static final class variables should be initialized along with their declaration..or

If they arent ...initialized..

then you can initialize...the variable in static initializer block..

else compilation error
Amit Sharma
Ranch Hand

Joined: Nov 22, 2005
Posts: 87
Thanx Mr. Saha. but why we get error if we initiliaze them frm a constructor.and not in case of final instance variable.
Purujit Saha
Ranch Hand

Joined: Nov 01, 2005
Posts: 86
As we know, each object of a class contains a copy of all the instance variables but not the static variable.Static variables are always alined with the class, so they have only one copy.

Now again as we know final variables can't be reinitialised. right ?.

So, when ever you call a constructor an object is created and at the same time all the instance variables are initialise for each object.So,in the above case each object will have their own copy of final instance variable whenever you call the constructor.

But, in case of static variable the picture is different.Static variables will not be created for each object. They are all class variable & initialised onetime,during class loading.So, if you initialise static final variables into a constructor then definately it will throw exception stating final variables can't be reinitialised.

I hope this will help you.

[ December 02, 2005: Message edited by: Purujit Saha ]

[ December 02, 2005: Message edited by: Purujit Saha ]
[ December 02, 2005: Message edited by: Purujit Saha ]
Amit Sharma
Ranch Hand

Joined: Nov 22, 2005
Posts: 87
thanx Mr. Saha for a detailed explanation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final static variable