• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

final static variable

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
___________________________________________________________________________
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx Mr. Saha for a detailed explanation
reply
    Bookmark Topic Watch Topic
  • New Topic