04 OOP - Lecture#4
04 OOP - Lecture#4
5 من1 الصفحة
5 من2 الصفحة
Figure 1. Instance variables belong to the instances and have memory storage independent
of one another. Static variables are shared by all the instances of the same class.
The Circle class defines the instance variable radius and the static variable
numberOfObjects, the instance methods getRadius, setRadius, and
getArea, and the static method getNumberOfObjects. (Note that static
variables and methods are underlined in the UML class diagram.)
o To declare a static variable or define a static method, put the modifier
static in the variable or method declaration.
o The static variable numberOfObjects and the static method
getNumberOfObjects () can be declared as follows:
2. Constants
o Constants in a class are shared by all objects of the class.
o Thus, constants should be declared as final static.
o For example, the constant PI in the Math class is defined as:
final static double PI = 3.14159265358979323846;
5 من2 الصفحة
5 من3 الصفحة
5 من3 الصفحة
5 من4 الصفحة
5 من4 الصفحة
5 من5 الصفحة
5 من5 الصفحة