Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static and final

 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello ranchers

wanted to know a thin-line difference between STATIC and FINAL, though both are using for a CONSTANT(GLOBAL) PURPOSE (almost like C++). is that something that final adds more means to static or it's for class as well so that class can't further extend.

that would great if i have (FINAL) concept for variable, method and class.


thank you friends

- chintan ramavat
[ February 10, 2007: Message edited by: Bear Bibeault ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34375
345
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chintan,
"static" means one per class. It is the opposite of an instance field/method. "final" means that you cannot extend the class.

Constants are generally both. You want them final so nobody can change the values. And since they aren't changing, you only need one copy.
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is also true for methods and variables

Final

Method
Method cannot be changed by subclasses

Variable
When a variable is given a value that value cannot be changed.

Static
Method
The method belongs to the class and not to the objects which are created. This means al the objects from the class share the same method. Also you can use the methods directly without making a object

Variable
The variable belongs to the class so al objects share the same variable en can access it. So if Object A of the Class changes a static variable an Object B of the same class will see the change.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello "Wilhem Tell",
welcome to JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name.
Thanks, and have fun!
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you ranchers I've got it cleared
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something they didn't tell you is why the name static is used. The data are in a static location in memory, unlike instance variables which are dynamic. Instance variables are put into memory when they are created, and can be removed again when they are finished with. Static data on the other hand remains in memory until the JVM terminates.

As far as I know, a static member is loaded into memory once and once only when the class is loaded, which is actually before execution starts.
That is why there is only one copy of each static member. So all instance of the class have access to that same member.
But when the static member is loaded, the instance members are not yet in memory. So a static method cannot gain access to the instance methods or fields.
Since "this" and "super" refer to the present instance of the class, or that proportion of the present instance of the class which is inherited, they are treated rather like instance members. So you can't use "this" or "super" in a static method.

People have told you about final. There are two sorts of final variables; constants (usually like this) or variables which cannot be changed after they are initialised. As far as I can remember, a final instance field must be initialised not later than the constructor call, and cannot then be reassigned.
There are also final local variables and method call parameters, but I shall keep quiet about them.

[edit]Minor spelling errors and bits missed out[/edit]
[ February 11, 2007: Message edited by: Campbell Ritchie ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic