File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Is final variable implicitly static ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is final variable implicitly static ?" Watch "Is final variable implicitly static ?" New topic
Author

Is final variable implicitly static ?

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 357
final is a constant whose value can not be changed. So, if a final variable is declared at class level and assigned value there, does it become static as well ?

Do final variables have to be declared at class level only OR can they be declared at local level also ? Does a final variable has to be initialized when its declared or can it be initialized anywhere ?

Thanks
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

final and static are completely different things. final means you can't change the value, means declaration and initialization comes in the same place and you can't reinitialize it. it can always be a method local variable as well.

static on the other hand is a class level variable. Declaring a class level variable as final does not make it implicitly static. you have to explicitly mention it as static.



Swastik
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Read what the language specification has to say: http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Swastik Dey wrote:final means you can't change the value, means declaration and initialization comes in the same place

Nope. Go through the link posted above ^^^
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

Sorry about that, I was bit wrong in explaining. My idea was to mention re initialization is not allowed.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 481
    
    1

Hello!

I was also confused when I met real sense of static.
Static variable has always the same value for all instances regardless when instances were created.
Copy paste this code to see static in action:


However I still not always understand when it is good practice to use static method and when to use static inner class?
It would be great if someone explained this too.


True person is moral, false is right!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7045
    
  16

nirjari patel wrote:final is a constant whose value can not be changed.

Actually, final has lots of uses; but for a variable, it's a value which can be set precisely once, and after that cannot be changed.

For an instance variable, it can either be set eagerly (ie, with '=') or in a constructor. For a static variable, it is normally initialized eagerly, but you may also be able to set it in a static initializer (don't know because I've never tried it; but I would expect so).

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is final variable implicitly static ?
 
Similar Threads
Pl Validate the points
final static variable
Aquestion about type of variable
static modifier
Facing null pointer exception in static initialization