I am a beginner in Java Programmer and do not understand what is this variable: public final static int ID = 8;
please tell me a definition or example code. Thanks.

- 1
- 2
-
3Have you tried searching on the web for those keywords? – squiguy Oct 31 '13 at 23:21
-
It's a 'magic number' !!! (really). – rolfl Oct 31 '13 at 23:23
-
@squiguy yes, but I did not find a clear definition. – Gent Tneg Oct 31 '13 at 23:23
-
Take a look at http://stackoverflow.com/questions/11677670/when-exactly-are-we-supposed-to-use-public-static-final-string – DanGar Oct 31 '13 at 23:23
-
It's essentially a constant to your class. – qwertyuiop5040 Oct 31 '13 at 23:23
3 Answers
public
- Any object can see it, even objects that use your code as a library.
final
- its value will never change.
static
- however many objects of this class you create, there will be only one.
int
- it is a 32-bit integer.
ID
- it can be referred to by this name.
=
- it is immediately assigned the value.
8
- it will have the value 8 (decimal).
This form is commonly used for constant values. The compiler will often replace every access to it with its constant value instead.

- 64,482
- 16
- 119
- 213
public
means that it can be accessed from other classes
final
means it cannot be reinitialized i.e its value can't be changed after initialization.
static
means that all instances
of the class use the same exact field (unlike non-static fields where each instance has their own version of the field). static
fields are described as being 'class variables' Similarly, non-static fields are called 'instance variables'
public
means it accessible by any other class outside of that one.
final
means once the variable is declared it cannot be changed.
static
means the variable can be accessed from any method within the class.
int
is a primitive data type declaration.
These are all basic concepts of Java OOP, so I'd recommend reading about it a little.
Declaring Variables: http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

- 952
- 1
- 13
- 25
-
5
-
Static means that the variable is not associated with an instance but with the class itself. – Daniel Larsson Oct 31 '13 at 23:26
-
Yeah sometimes people rush in a bit for free reps but instead get a kickback – Prateek Oct 31 '13 at 23:27
-
Static means something different from final I know, but if you declare a static variable outside of any method, it doesn't make that much of a difference for its usage. It just means that the variable belongs to the class not any particular method or constructor. – Wold Oct 31 '13 at 23:29
-
1@Wold - you are wrong on this one, and the subtle difference is significant: See http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class – rolfl Oct 31 '13 at 23:30
-
-
@rolfl I admit my misunderstanding of the implications of a static variable. I just thought that it seemed intuitive that a variable both final and static would not exist only once. – Wold Oct 31 '13 at 23:33