There will be times when you will want to define a class member that will be used independently of any object of that class. Normally a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keywork static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main(), main() is declared as static because it must be called before any objects exist.
Instance variables declared as static are, essentially, global variables. When objects of its class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.
Methods declared as static have several restrictions:
1.They can only call other static methods.
2.They must only access static data.
3.They cannot refer to this or super in any way.
Remeber: It is illegal to refer to any instance variables inside of a static method
Final:
A variable can be declared as fina. Doing so prevents its contents from being modified. This means that you initialize a final variable when it is declared.
本文介绍了在面向对象编程中如何使用静态成员,包括静态方法和静态变量,并解释了它们的特点与限制。此外,还讨论了final变量的概念及其用法。

被折叠的 条评论
为什么被折叠?



