Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Object-oriented static

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Static

Static variable

Static variables are also called static variables. The difference between static variables and non-static variables is that static variables are shared by all objects, have only one copy in memory, and are initialized only when the class is first loaded. Non-static variables are owned by the object, are initialized when the object is created, there are multiple copies, and the copies owned by each object do not affect each other.

The initialization order of static member variables is initialized in the order defined.

Static method

Static methods are generally called static methods. Because static methods can be accessed without relying on any object, there is no this for static methods, because they are not attached to any objects. Since there are no objects, there is no this. And because of this feature, the non-static member variables and non-static member methods of the class cannot be accessed in static methods, because non-static member methods / variables must rely on specific objects before they can be called.

It is important to note that although non-static member methods and non-static member variables cannot be accessed in static methods, static member methods / variables can be accessed in non-static member methods. Take a simple example:

Class MyObject {

Priative static String strl- "staticProperty"

Priative static str2- "property"

Public MyObject {

}

Public void print1 () {

System.out.println (str1)

System.out.println (str2)

Print2 ()

}

}

Static code

When the class is loaded, the static code block executes, and because the class is loaded only once, the static code block executes only once.

Lines of code surrounded by a pair of curly braces are called a code block, and a code block decorated with the static keyword is a static code block.

Single column mode

The constructor of the class is decorated with private and declared private so that the instance object cannot be created using the new keyword outside the class

Create an instance object of the class inside the class and use the static variable instance to reference the object. Since the variable should not be directly accessed by the outside world, it is decorated with private and declared as a private member.

In order to obtain the instance object of the class outside the class, we need to define a static method getInstance (), which is used to return the instance instance of the class. Because the method is static, the outside world can use the class name. Method name to access the.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report