OOP 04 Static Fields and Static Methods - Tài liệu tham khảo | Đại học Hoa Sen

OOP 04 Static Fields and Static Methods - Tài liệu tham khảo | Đại học Hoa Sen và thông tin bổ ích giúp sinh viên tham khảo, ôn luyện và phục vụ nhu cầu học tập của mình cụ thể là có định hướng, ôn tập, nắm vững kiến thức môn học và làm bài tốt trong những bài kiểm tra, bài tiểu luận, bài tập kết thúc học phần, từ đó học tập tốt và có kết quả cao cũng như có thể vận dụng tốt những kiến thức mình đã học.

Static Fields and
Static Methods
Contents
Stac elds and Object fields
Stac methods and Object methods
Constants
References
Chapter 9. Liang. (2015). Introducon to Java Programming,
10th Edion. Pearson.
hps://docs.oracle.com/javase/tutorial/java/javaOO/classvars.
html
18/10/2023 Object-Oriented Programming (Java) 2
Static Fields
A stac variable is shared by all objects of the class.
If you want all the instances of a class to share data
use stac variables,
also known as class variables.
A stac field of all instances have same memory
locaon
How to call/access an stac variable
ClassName.staticVariable
Circle.numberOfObjects;
objectName.staticVariable
c1.numberOfObjects;
18/10/2023 Object-Oriented Programming (Java) 3
Static Fields
Because of this common location, if one object changes
the value of a stac variable, all objects of the same
class are affected.
18/10/2023 Object-Oriented Programming (Java) 4
Example 1
18/10/2023 Object-Oriented Programming (Java) 5
Example 1 (cont.)
18/10/2023 Object-Oriented Programming (Java) 6
Static Methods
Stac methods can be called without creang an
instance of the class.
A stac method cannot access instance members (i.e.,
instance data elds and methods) of the class.
To invoke a stac method
ClassName.methodName(arguments)
To access a stac variable
ClassName.stacVariable
All the methods in the java.lang.Math class are stac.
Ex. Math.random();
18/10/2023 Object-Oriented Programming (Java) 7
public static void main() method
The method is main() stac, too.
Error
18/10/2023 Object-Oriented Programming (Java) 8
Static Methods
18/10/2023 Object-Oriented Programming (Java) 9
Static Methods
18/10/2023 Object-Oriented Programming (Java) 10
| 1/32

Preview text:

Static Fields and Static Methods Contents
 Sta c elds and Object fields
 Sta c methods and Object methods  Constants  References
 Chapter 9. Liang. (2015). Introduc on to Java Programming, 10th Edi on. Pearson.
 h ps://docs.oracle.com/javase/tutorial/java/javaOO/classvars. html 18/10/2023
Object-Oriented Programming (Java) 2 Static Fields
 A sta c variable is shared by all objects of the class.
 If you want all the instances of a class to share data
 use sta c variables,
 also known as class variables.
 A sta c field of all instances have same memory loca on
 How to call/access an sta c variable  ClassName.staticVariable • Circle.numberOfObjects;  objectName.staticVariable • c1.numberOfObjects; 18/10/2023
Object-Oriented Programming (Java) 3 Static Fields
 Because of this common location, if one object changes
the value of a sta c variable, all objects of the same class are affected. 18/10/2023
Object-Oriented Programming (Java) 4 Example 1 18/10/2023
Object-Oriented Programming (Java) 5 Example 1 (cont.) 18/10/2023
Object-Oriented Programming (Java) 6 Static Methods
 Sta c methods can be called without crea ng an instance of the class.
 A sta c method cannot access instance members (i.e.,
instance data elds and methods) of the class.  To invoke a sta c method
 ClassName.methodName(arguments)  To access a sta c variable  ClassName.sta cVariable
 All the methods in the java.lang.Math class are sta c.  Ex. Math.random(); 18/10/2023
Object-Oriented Programming (Java) 7
public static void main() method
 The main() method is sta c, too. Error 18/10/2023
Object-Oriented Programming (Java) 8 Static Methods 18/10/2023
Object-Oriented Programming (Java) 9 Static Methods 18/10/2023
Object-Oriented Programming (Java) 10