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

How to use Static in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to use Static in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Literal meaning of static: static

What static means in the program, static, does not need to be instantiated

Static is an adjective that means to modify something, including {methods, variables, code blocks}

The method modified by static:

You can use the class name directly. Method name invocation does not require class instantiation.

Static decorated methods cannot contain anything non-static. This contradicts the definition of static.

It is possible to call static methods in non-static methods, and vice versa.

Variables modified by static:

The static variable is shared by all objects, has only one copy in memory, and is initialized when and only when the class is first loaded. Non-static variables are owned by the object, are initialized when the object is created, and there are multiple copies, which are not affected by each copy.

The example of static

The main method, the main method is the entry for the function to be called by the system, and if the main method needs to be called after instantiation, it cannot be used.

two。

From the following example, you can see that the object can be accessed directly in StaticModifierTest, and the object is assigned a value of 10

StaticModifier.java

Package cp5.zgthlife.cn

Public class StaticModifier {

Public static int number = 0

}

StaticModifierTest.java

Package cp5.zgthlife.cn

Public class StaticModifierTest {

Public static void main (String [] args) {

StaticModifier.number = 10

System.out.println (StaticModifier.number)

}

}

Thank you for reading! This is the end of the article on "how to use Static in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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

Development

Wechat

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

12
Report