0% found this document useful (0 votes)
4 views5 pages

04 OOP - Lecture#4

Uploaded by

Abdunnaser Diaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

04 OOP - Lecture#4

Uploaded by

Abdunnaser Diaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

5 ‫ من‬1 ‫الصفحة‬

Static variables, Static Methods and Constants


A static variable is shared by all objects of the class. A static method cannot access
instance members of the class.

In this lecture, the following topics are covered:


 Static Variables in Java
 Static Methods in Java
 Constants in Java

1. Static Variables and Methods in Java


o The data field radius in the circle class (introduced in the class
definition and object creation lecture) is known as an instance
variable.
o An instance variable is tied to a specific instance of the class; it is not
shared among objects of the same class. For example, suppose that
you create the following objects:

o The radius in circle1 is independent of the radius in circle2 and is


stored in a different memory location. Changes made to circle1’s
radius do not affect circle2’s radius, and vice versa.
o If you want all the instances of a class to share data, use static
variables, also known as class variables.
o Static variables store values for the variables in a common memory
location.
o If one object changes the value of a static variable, all objects of the
same class are affected.
o Java supports static methods as well as static variables. Static
methods can be called without creating an instance of the class.

5 ‫ من‬1 ‫الصفحة‬
5 ‫ من‬2 ‫الصفحة‬

Let’s modify the Circle class by adding a static variable numberOfObjects


to count the number of circle objects created. When the first object of this
class is created, numberOfObjects is 1. When the second object is created,
numberOfObjects becomes 2. The UML of the new circle class is shown in
Figure 1.

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 ‫الصفحة‬

3. The Circle class with static members


The new circle class, named CircleWithStaticMembers, is defined in Listing 1.
Listing1 . The Circle class with static members.

o Method getNumberOfObjects () in CircleWithStaticMembers is a


static method.
o Instance methods (e.g., getArea ()) and instance data (e.g., radius)
belong to instances and can be used only after the instances are
created. They are accessed via a reference variable.
o Static methods (e.g., getNumberOfObjects ()) and static data (e.g.,
numberOfObjects) can be accessed from a reference variable or from
their class name.

5 ‫ من‬3 ‫الصفحة‬
5 ‫ من‬4 ‫الصفحة‬

Use ClassName.methodName(arguments) to invoke a static method and


ClassName.staticVariable to access a static variable. This improves
readability, because this makes the static method and data easy to recognize
The program in Listing 2 demonstrates how to use instance and static
variables and methods and illustrates the effects of using them.
Listing2 . A test class for CircleWithStaticMembers Class.

o An instance method can invoke an instance or static method and


access an instance or static data field.
o A static method can invoke a static method and access a static data
field.

5 ‫ من‬4 ‫الصفحة‬
5 ‫ من‬5 ‫الصفحة‬

o However, a static method cannot invoke an instance method or


access an instance data field, since static methods and static data fields
don’t belong to a particular object.
The relationship between static and instance members is summarized in
the following diagram:

5 ‫ من‬5 ‫الصفحة‬

You might also like