Читайте также: |
|
Members are fields and methods of a class.
Fields and methods of a class may be static or non-static. Until now we have used only non-static members.
Non-static members are always connected with an existing object (fields represent data stored in the object, methods are messages sent to the object and must be called for the object).
Static members (fields and methods):
Static members may be accessed with the class name followed by the dot and the field name:
AClassName.AFieldName
In the class System (contained in J2SDK) there is static method exit(int) which terminates the execution of the application (and returns the given return code).
Thus, to terminate the program with the exit code 0 we write:
System.exit(0);
Earlier we have used methods displaying dialogs:
JOptionPane.showInputDialog("Give some data")
JOptiomPane.showMessageDialog(null, "Message")
They are static methods of the class JOptionPane. Thanks to this, we can call them using the class name - JOptionPane.
The method parseInt, which we used to convert strings to numbers, is a static method of the class Integer. Therefore we wrote:
Integer.parseInt("123");
Now the most confusing puzzle.
The class System has a static field named out which specifies the standard output of the program (the command prompt). This object understands the message println which causes printing the argument as a string to the command prompt (println is the method of the class PrintStream; out is the object of the class PrintStream).
So we write System.out.println(...)..
Only the variables which are fields of classes may be declared static.
It is very important to realize the difference between static and non-static members.
Static members describe properties of the class, not its objects.
Particularly, in the case of fields the difference is as follows:
For example, in a class describing hard disks we have a static field named vat and non-static fields: model, capacity, price. Disks differ in model, capacity and price but have the same value of the VAT, so there is no need for storing it in each object separately. It is a common property of all disks. Being a static variable it occupies only 8 bytes of memory space regardless of the number of disks (objects) created in the program.
public class Disk { private static double vat; private String model; private int capacity; private double price; public Disk(String m, int c, double p) { model = m; capacity = c; price = p; } public String getDescription() { return model + "," + capacity + " MB"; } public double getGrossPrice() { return price * (1 + vat/100); } public static void setVat(double v) { vat = v; } } class Test { public static void main(String[] args) { Disk.setVat(22.0); Disk d1 = new Disk("IBM598", 6040, 430.0); System.out.println(d1.getDescription() + " - price " + d1.getGrossPrice() + " EUR"); }}IBM598,6040 MB - price 524.6 EUR
Note that the method setVat had been called before any object was created. It set the value of the field vat, although no object of the class Disk existed.
Static methods cannot access non-static members of the class (as no object may exist). Other static members may be accessed.
Of course, non-static methods may access static members in a normal way (see the method getGrossPrice()).
Дата добавления: 2015-11-16; просмотров: 45 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Defining methods of object creation (constructors) | | | Packages and imports |