Lindse
Lindse

Reputation: 45

Why should a variable be declared as static and final

A variable is declared as static to get the latest and single copy of its value; it means the value is going to be changed somewhere. But why should the same variable be declared as final, which will not allow the variable to be changed else where (constant value)?

Upvotes: 2

Views: 11672

Answers (12)

Basheer AL-MOMANI
Basheer AL-MOMANI

Reputation: 15357

Variables should be declared as fields only if they’re required for use in more than one method of the class or if the program should save their values between calls to the class’s methods.

for example user.lastName lastName should be field because it is needed during object lifecycle

Variables should be declared as static only if they’re not required for use in more than one method of the class or if the program should not save their values between calls to the class’s methods.

for example Math.max(num1,num2) Im not intristed in num1 and num2 after compleating this operation

Upvotes: 1

Adeel Ansari
Adeel Ansari

Reputation: 39907

May be to provide something similar to constants.

Upvotes: 1

midhunhk
midhunhk

Reputation: 5554

static so that the variable or method can be accessed without creating a class instance, and there is only one variable for the class instead of one for each instance.

A final class cannot be extended. A final variable cannot have its value changed, it behaves as a constant. And a final method cannot be over-ridden.

Upvotes: 11

Dhruv Gairola
Dhruv Gairola

Reputation: 9182

static final is used in Java to express constants. Static is used to express class variables, so that there is no need to instantiate an object for that class in order to access that variable.

Final methods can't be overriden and final variables can only be initialised once.

If you only use the static keyword, that value will not be a constant as it can be initialised again.

Upvotes: 1

Costi Ciudatu
Costi Ciudatu

Reputation: 38255

Static has nothing to do with getting the latest and single copy unless "single copy" here means one and the same value for all the instances of a class (however, I think you may be confusing it with volatile). Static means class variable. You make it final when you want that to be a constant (that's actually the way Java constants are declared: static final).

Upvotes: 1

Nick
Nick

Reputation: 5805

The minute a variable is defined as final, it should probably not be referred to as "variable", since it no longer "varies" :)

A static variable is not tied to any particular instance of a class -- it is only tied to the class itself and only from a scoping standpoint.

So there you are -- a static and final variable is actually a value that is not tied to any particular instance of class and does not vary. It is a constant value, to be referenced from anywhere in your Java code.

At some point, when you should decide to change the value of this constant, it only takes one change to propagate this change correctly to all other classes that use this constant.

Upvotes: 3

Kwebble
Kwebble

Reputation: 2075

Like you mention yourself, this is done to create constants. You create a single field to hold a value with a specific meaning. This way you don't have to declare that value everywhere, but instead you can reference the static.

Upvotes: 1

LoSciamano
LoSciamano

Reputation: 1119

You create static final variable to make its value accessible without instantiating an object. E.G.:

public class MyClass
{
     public static final String endpoint= "http://localhost:8080/myClass":
     /* ...*/
}

Then you can access to the data using this line:

MyClass.endpoint

Upvotes: 0

ewan.chalmers
ewan.chalmers

Reputation: 16255

static fields can be modified (e.g. public static fields can be modified by any class). static final fields cannot be modified after initialization.

Upvotes: 1

Wytse
Wytse

Reputation: 500

A variable declared as static means that its value is shared by all instances of this class. Declaring a variable as final gives a slightly better performance and makes your code better readable.

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533790

local variables are on the stack and are not static.

You can have a static field which may or may not be final. You would make the field final if it is not going to change.

Upvotes: 1

Tom Squires
Tom Squires

Reputation: 9296

Final stops any classes inheriting from it

Upvotes: 0

Related Questions