questions on static variables in java
i was trying to use static variables in my programs and understood that static variables means to be using the same instance of the variable everytime.
however, i have been using static variables because i felt that it allows me to get variables that i need without 'instantiating' (new) the class. or specifically because i need the variable in another class and i shouldn't be creating a new instance of the class.
is this the right way to do it?
and should i be creating static GET methods to get static variables declared in the 开发者_运维问答class?
..., i have been using static variables because i felt that it allows me to get variables that i need without 'instantiating' (new) the class
Declaring a variable static does not mean you bypassed new. It simply means that the variable refers to one, and only one instance of its class.
Example:
class Universe {
//there is only one Earth, but it needs to be constructed. In this example, when
//the Universe class is loaded, the initialization below happens.
static World EARTH = new World(...);
public static Planet getTheEarth() {
return EARTH;
}
}
or specifically because i need the variable in another class and i shouldn't be creating a new instance of the class.
OK, so pass the reference to the variable to the other class.
Example:
class Earth {
Moon moon;
public Earth(Moon moon) {
this.moon = moon;
}
}
Static variables are used if you want to have 1 value shared among all instances of a class. E.g. if you have something like
public class Person {
public static int instancesCount = 0;
public Person() {
Person.instancesCount++;
}
}
This means for each instance of Person the "global" instanceCount variable would be incremented by one. So if you do
Person p1 = new Person();
Person p2 = new Person();
then both of those instance s(p1 and p2) would have access to Person.instanceCount variable and they both will show number 2. Furthermore, if you modify the variable in either of the instances then both will have the same visibility of it.
Basically, don't use static variables unless you know what you are doing.
First off, you should not be using GET methods to get static variables declared in the class. If for instance you wanted to print the static variable you do something like this:
static int staticVariable = 0;
ClassObject c;
System.out.println(c.staticVariable);
Basically any object of the class can access the static variable, and all you have to do is access it using an instance of the class. I recommend staying away from static variables until you have a better understanding of them.
What do you mean by "need the variable in another class"? Why "shouldn't you be creating a new instance"? Why is the variable "in" the other class in the first place?
Classes are not storage vessels for code and/or data. They are definitions of new data types. Hence the whole 'instance' thing.
If you are just using your class told hold data that is accessible by other classes then you probably don't want to use static fields. If you want to ensure that you only have one instance of this class, it is probably better and more flexible to use the singleton pattern. Static fields should general only be used if you want all instances of of a class to share that field.
You really need to get some basic Object Oriented Programming concepts. Start by a book like "Thinking in Java" or "Effective Java".
精彩评论