Viku
Viku

Reputation: 2973

constant data member

when we use final keyword that variable has been declared as constant then what is the necessity of using static ? I have seen in most of the places that we use

public static final int nVar = 12

for constant data member .

Upvotes: 3

Views: 2991

Answers (6)

Bohemian
Bohemian

Reputation: 424953

final means the variable may not be reassigned to another object/primitive.

static means that all code running in the JVM shares the same variable. It exists once per class, instead of once per object.

A final non-static variable can not be reassigned, but each instance has its own copy.

static final fields (accessible without creating an instance) are generally called "Constants"

final (non static) instance variables are generally called "Immutable fields"

Upvotes: 5

CloudyMarble
CloudyMarble

Reputation: 37566

declaring it static enables you accessing the variale without creating an ojbect of the type .

Upvotes: 2

Rahul
Rahul

Reputation: 16335

final means that, once assigned the value of variable cannot be modified.

static means "associated with the class"; without it, the variable is associated with each instance of the class. if not, you'll have one for each instance you create. static means the variable will remain in memory for as long as the class is loaded

There is no point in declaring a variable like this.

public final int nVar = 12;

If this is not meant to be modified, why have one copy per instance.

Hence, Class constants need to be declared as static final where as the variables which you want to be immutable on per instance basis, you declare them as final

Upvotes: 4

NoNaMe
NoNaMe

Reputation: 6222

One reason could be that you do not need to create an object of the class to access that constant that is why you should/may declarer it as static. As you can access it with class name as it is static.

Check this code

public class A{
public static final int aa = 1;
}

public class B{
public final int bb = 1;
}

public class Testing{
SOP(A.aa);// a can be accessed with class name
B b = new B();// where to access the constant bb we need to create class object 
SOP(b.bb);
}

Upvotes: 1

Renjith
Renjith

Reputation: 3274

A static variable means it is available at class level.Only one instance of that variable will be available for all objects of that class.Static variable can be modified, but the change will reflect in all objects of that class. A final variable means its value cannot be change after initialization. A final variable with static modifier means the variable is available at class level.In short, it will act as a constant for all objects of that class

Upvotes: 0

EJK
EJK

Reputation: 12527

The "final" identifier means that the variable's value cannot be changed.

"static" however means that there is one instance of the variable within the class that it is defined.

So final and static are 2 different things, but you often see them used together to define constants for a class.

Upvotes: 1

Related Questions