wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Final vs. Static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Final vs. Static" Watch "Final vs. Static" New topic
Author

Final vs. Static

Jennifer Neale
Greenhorn

Joined: Oct 08, 2003
Posts: 3
Can someone explain the difference?
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
final declares a variable to be constant. Which means once you set its value, it never changes and trying to change it gives an error. An example of using final is:
final int i = 0;
i is equal to 0, it will always be equal to 0 and can never be changed.

static is a completely different animal. static just means that this variable exists outside of any class. They are usually referenced via class name 'dot' variable name like so:
static stVar is declared in Class1. It is a member of that class but can be accessed even if no instances of Class1 exist. It can be accessed with Class1.stVar.
Hope this helps,


Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="http://alia.iwarp.com" target="_blank" rel="nofollow">http://alia.iwarp.com</a>
Jennifer Neale
Greenhorn

Joined: Oct 08, 2003
Posts: 3
Thanks, Jason - that makes sense.
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
Glad to be of service.
Best Regards,
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
Jennifer: In addition to Jason's excellent post, here is some more information about final and static.
The keyword final is used to prevent a method from being overridden, or to prevent a class from being inherited. It can also be used to create variables whose values can't be changed, as Jason points out.
The static keyword can be used to create a variable that is shared by all instances of its class. Thus, there is only one copy of a static variable that will be used by all objects of the class. As Jason points out, it can be accessed independently of any object. In essence, a static variable is similar to a global variable in other computer languages.
You can also create static methods. A static method can call only other static methods, access only the static data defined by its class, and not use this or super.
[ October 15, 2003: Message edited by: Herb Schildt ]

For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Jennifer
As Jason pointed out finals are constansts when we talk about variables(ints, floats, etc.).
Like say

Now
would be wrong because a has been declared as final.
You need to intialize a final variable declared inside a class but outside of any method in a constructor(for non-static variables) if not already intialized in at the time of declaration or in a static intializer(for static variables).
Similarly when you declare a method as final that method can not be overridden(but can be ooverloaded). As for classes final classes can not be subclassed.
When you think about final think complete, full.
Static variables you can say are class variables.
When you mark a variable as static the varibale gets intialized when the class is intialized. Though there may be many instances of a class, there can only be a single static variable.
That is

Now when the class Test would be intialized a and b would also be intialized to 10 and 100. Secondly you may have hundreds instances of class Test but you can only have a single a and single b. Static variables can not be declared inside of a method.
Methods like static variable are associated with a class. You can not override(but again can overload them) a static method. Static methods can not access a not static variable.
Satic variables can be accessed like

Though this is also valid but my cause confusion.

Now as we only have a single a variable the value of a is 200. So it's a good idea to access static variables though their class names.
Satic methods can also be invoked in the above manner.

[ October 15, 2003: Message edited by: Anupam Sinha ]
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
Originally posted by Herb Schildt:
Jennifer: In addition to Jason's excellent post, here is some more information about final and static.

Thank you for the compliment.
Best Regards,
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Allow me to suggest a change of explanation.
static just means that this variable exists outside of any class.
I'd tend to think that static means the variable exists separate of any class instance, and that it does in fact belong to the one and only class (ignoring multiple class loaders). Whereas an instance variable is owned by a single class instance.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Lisa D'Aniello
Ranch Hand

Joined: Sep 25, 2003
Posts: 42
I am glad this post came up, because I have trouble with this as well (along with many other concepts in Java), and the replies that I've read to the initial question made me consider two more. When is the static variable "reset"? Also, in a jsp page that I have been experimenting with, I can only access a classes methods via <%= class.method() %> if the method is declared static, but I would like to return an error string that is shared in all of the methods of the class and instance specific. This is no good. Am I missing something?
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Reiterating on the Static topic: Static data and methods go with the class and not specific instances of the class. Since they exist outside of instances, they are unable to access any other data or methods that exist only in instances. Memory for static data an methods is set aside when the class is loaded. Memory for non static data is not. It is allocated for a particular instance when that instance is instantiated.
This is why static methods and data cannot access not static methods and data - they don't exist in the static context.
Another point about final data. It has been said correctly that final data and methods cannot be changed once declared. However all the examples have been combination declaration and initialization statements. I don't want this to give you the wrong idea. The following is also correct and will create a value that cannot be changed:

Final variables can be declared but not initialized. Later they can be assigned a value. Once assigned a value, final variables cannot be changed.


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Final vs. Static