アンダstanding Instance and Class Members(Reprint from)http://download.oracle.com/javase/tutorial/ja)
アンダstanding Instance and Class Members
In thisセレクション、we discuss the use of the
static
keyword to create fields and methods that belong to the class、rather than to an instance of the class.Class Varabes When a number of object s are created from the same class blueprint,they each have their own distinct copies of
instance variables.In the case of the
Bicycle
class、the instance variables arecadence
、gear
、andspeed
・EachBicycle
object has its own values for these variables、stored in different memory locations.Sometimes、You want to have variables that are comon to all object.This accomprise hed with the
static
modifier.Fields that have thestatic
modifier in their declaration aration ares.Therather than with any object.Every instance of the class shares a class variable,which is in one fixed location in memory.Any oject can change the value of a class variable,but class variable can also be the ables can alsobe the pro night ablence of cant cant cant cant cant ables。For example、suppose you want to create a number of
Bicycle
object and assign each a serial number、begining with 1 for the first object.This ID number is to Aque to each oject and and therefore instance Avanceyou need a field to keep track of how manyBicycle
object have been created so that you know w what ID to asign to the next one.Such a field is not related to any individual oject、but the clas fone.Foasspublic class Bicycle{
private int cadence;
private int gear;
private int speed;
// add an instance variable for the object ID
private int id;
// add a class variable for the number of Bicycle objects instantiated
private static int numberOfBicycles = 0;
......
}
Class variables are referenced by the class name itself,as in Bicycle.numberOfBicycles
This makes it clear that they ars class variables.ノート: You can also refer to static fields with an object reference like
myBike.numberOfBicycles
but this is discouraged because it does not make it clear that they are class variables.You can use the
numberOfBicycles
construct to set theBicycle
instance variable and increment theid
class variable:public class Bicycle{
private int cadence;
private int gear;
private int speed;
private int id;
private static int numberOfBicycles = 0;
public Bicycle(int startCadence, int startSpeed, int startGear){
gear = startGear;
cadence = startCadence;
speed = startSpeed;
// increment number of Bicycles and assign ID number
id = ++numberOfBicycles;
}
// new method to return the ID instance variable
public int getID() {
return id;
}
.....
}
Class Methods The Java programming langage supports static methods as well as static variables.Static methods,which have thenumberOfBicycles
modifier in their declarations、shuld be invoked with the class name、without the need for creating an instance of the class、as inClassName.methodName(args)
ノート: You can also refer to static methods with an object reference like instanceName.methodName(args)
but this is discouraged because it does not make it clear that they are class methods.A common use for static methods is to access static fields.For example、we could add a static method to the
static
class to access theBicycle
static field:public static int getNumberOfBicycles() {
return numberOfBicycles;
}
Not all commbinations of instance and class variables and methods are allowed:numberOfBicycles
keyword there is instarenceコンスタントThe
this
modifier、in commbination with thethis
modifier、is also used to define constants.Thestatic
modifier indicates that the value of this field cannot change。For example、the follwing variable declaration defines a constant named
final
、whose value is approtions of pi(the ratio of the circermaference of a circe to its diameter):static final double PI = 3.141592653589793;
Conts defined in this way cannot be reassigned,and it is a comples-time error ft your program tries to do so.By convent,the names of constastars values are spelled in up percase letters.If the name is compone。ノート: If a prmitive type or a string is defined as a constant and the value is known at comppile time,the compler replacces the constant name evere here in the code with its value.This is caled a
comple-time constant.If the value of the constant in the out side world changes(for example、if it is legisland that at pi shoult 3.975)、you will need to recomple any clases that use starrett.
The
final
Class After all the modifications made in this section、thePI
クラスis now:public class Bicycle{
private int cadence;
private int gear;
private int speed;
private int id;
private static int numberOfBicycles = 0;
public Bicycle(int startCadence, int startSpeed, int startGear){
gear = startGear;
cadence = startCadence;
speed = startSpeed;
id = ++numberOfBicycles;
}
public int getID() {
return id;
}
public static int getNumberOfBicycles() {
return numberOfBicycles;
}
public int getCadence(){
return cadence;
}
public void setCadence(int newValue){
cadence = newValue;
}
public int getGear(){
return gear;
}
public void setGear(int newValue){
gear = newValue;
}
public int getSpeed(){
return speed;
}
public void applyBrake(int decrement){
speed -= decrement;
}
public void speedUp(int increment){
speed += increment;
}
}