-
final VS static keywords
hey guys
im new to java finding it very interesting
but i have a little confussion
what is the different between final keyword and static keyword ?
i am using this statement
final static int LIGHTSPEED = 300000;
and i am getting compiler error, compiler is telling me that only final is required
i understand that any one can please explain me that different please.
thanks
-hisheeraz
-
My compiler doesn't give an error and I can't see why yours would. Static means that one copy is made for all instances of that class, while final means the variabele can't be modified. So they are completly different things.
-
Thanks for the reply
I'm using eclipse just downloaded today
When I remove static keyword from the statement then it is
Working fine. Where ad the book that I'm using to study java
Is using complete statement I.e
final static int LightSpeed =300000;
My book is The Java Way by Jerard Sparke
According to the author there should not be any error
But my comp won't let me go pass that line.
Thanks for your time any ways.
What should I do?
Regards
-hisheeraz
-
This is the time you show us a sscce, a short, self-contained, compilable example. Try to reproduce the error in the smallest amount of self-contained (=runnable) code possible and then show us that code. Then we can help you further.
-
I dont know your code but is that variable LIGHTSPEED in a method in your class. Any static variable shouldn't be in a method, it should be outside all methods in your class. when i put a static variable in a method eclipse shows a similar error message. try to put that LIGHTSPEED variable outside all of your methods and see if that works
correct way
Code:
public class YourClass {
final static int LIGHTSPEED = 300000;
public void exampleMethod() {
}
}
wrong way
Code:
public class YourClass {
public void exampleMethod() {
final static int LIGHTSPEED = 300000;
}
}
-
Roedy Green discusses static and final in his handy Java glossary.
-
When applied to variable, final static keywords make it a constant that is accessible from static context - in plain english, there is no need to create an object of the class where its located to access it. final makes the variable unchangeable at runtime - that is, makes it a constant.
So here is where it makes sense:
Code:
public class Universe {
public final static speedOfLight = 300000000;
}
Hence, any other object can get that variable as such: Universe.speedOfLight;
It makes sense to make speedOfLight in the universe a public final static as it is not something that could change at program runtime, and it should be accessed by all objects interacting with the Universe.
If however, you're coding a spaceship in that universe, it would not make sense to make its speed final, as it could change, or static, as other objects would probably not need to find that information if the specific object for the spaceship doesn't exist yet.
Look in the class for good examples on how to use finals and statics.
-
OMG, thank you all
especially Kyle227 for pointing out to me about the position of the statement
in my lousy code and also thanks to Maximus-EVG for explaining the difference
in plain english.
I was writing my statement inside the main method thats why i was getting that error
now that i have moved the statement out of the method and placed it in the class
it is working fine.
and of course everyone else who made an effort for me
cheers