Reputation: 1659
Sometimes I see something like :
public class MainActivity extends Activity
{
public static final String url_google = "http://www.google.com";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
What I actually don't get, is why using public static final
, and not public final
or final
Upvotes: 2
Views: 333
Reputation: 28866
I think A--C's answer is misleading: "I'm speaking very broadly, but if it's final, you only need one instance of it anyways, so it saves memory by making it static." As others pointed out he's mixing static and final which are two very different things. It's not broadly speaking IMHO but not precise enough, especially when explaining fundamental concepts.
Static variables are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.
Final variables on the other side are variables that can only be initialized once. Their values don't have to be known at compile time but can be set at run time. If a final variable is an instance variable there can be many instances of this variable, all with different values.
A static final variable is a constant because there's just one and it cannot be altered at run time once it has been initialized. In Java at least the value doesn't have to be known at compile time.
Now that is all very theoretic, so let's make an example to show the use for these types of variables:
public class Circle implements Serializable {
private static final long serialVersionUID = -1990742486979833728L;
private static int sNrOfCircles;
private final double mArea;
public Circle(double radius) {
sNrOfCircles++;
mArea = radius*radius*Math.PI;
}
}
serialVersionUID associates a version number with the Circle class used during deserialization to verify that the sender and receiver have loaded classes for that object that are compatible with respect to serialization. serialVersionUID never changes and is the same for all instances of Circle.
sNrOfCircles counts the number of Circle instances. It changes with every new instance of Circle (while serialVersionUID never does).
mArea defines the area of the Circle. It also changes with every instance of Circle but compared to sNrOfCircles it's associated with the Circle object and not the Circle class and also it can't be altered while sNrOfCircles will change when another Circle object is created.
To put it simple: use static if it's a Class attribute (how many Circles do we have?), use final if the value won't change once it's initialized (area of a circle with a given radius) and use static final if it's both.
Upvotes: 1
Reputation: 36449
I'm speaking very broadly, but if it's final, you only need one instance of it anyways, so it saves memory by making it static.
To be more specific, the final
keyword means that whatever the variable stores cannot be changed. This means that once the variable has a value, you may use the variable, but you cannot modify it in any way. Commonly, to give a value to a final variable, you do so right from the declaration eg final int variable = 12
. As you can see I used an int for my example, however you can use anything, including reference variables. Reference variables, are special though, because you cannot change what the variable points to, but you can change the Object itself (such as using get/set methods).
What this boils down to though, is that once you have made a final variable it occupies space in memory. Since we can't modify this variable further, why should we recreate it every time our Class is instantiated? So we use the static
keyword. This allows the variable to be created once, and only once in memory.
There are though, some specific cases in which you would want to not use static
and use just final. One example may be time sensitive variables, such as storing the time of Object instantiation.
Upvotes: 14
Reputation: 4199
The Java final
keyword is very loosely used to indicate that something "cannot change".
It has nothing to do with the static
keyword which indicated it's a “class variable” meaning all instances share the same copy of the variable. A class variable can be accessed directly with the class, without the need to create a instance.
They have different meaning and can be used together or separately.
Upvotes: 3
Reputation: 4357
The main reason would be that you an access the value without needing an actual instance of the class.
In your example, any code could access the url_google value, without needing an instance of MainActivity.
Upvotes: 0
Reputation: 117655
static
means that only one url_google
will be created. If it is an instance field (not static), then a new url_google
will be created with each instance of the activity, and what you actually need is only one copy of url_google
.
Upvotes: 3