-2

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.

Gent Tneg
  • 1
  • 2

3 Answers3

2

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.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

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'

rolfl
  • 17,539
  • 7
  • 42
  • 76
Epicblood
  • 1,167
  • 2
  • 10
  • 29
-2

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

Wold
  • 952
  • 1
  • 13
  • 25
  • 5
    That's not what static means – rolfl Oct 31 '13 at 23:24
  • 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
  • I kind of there is a HUGE difference though.. – Daniel Larsson Oct 31 '13 at 23:32
  • @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