Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final vs. Static

 
Jennifer Neale
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain the difference?
 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Jennifer Neale
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jason - that makes sense.
 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to be of service.
Best Regards,
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lisa D'Aniello
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic