my dog learned polymorphism*
The moose likes Beginning Java and the fly likes static and final Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static and final" Watch "static and final" New topic
Author

static and final

chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
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

Joined: May 26, 2003
Posts: 30749
    
156

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
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.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Thank you ranchers I've got it cleared
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static and final