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 solve the Static pit

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to solve the Static pit". In the daily operation, I believe many people have doubts about how to solve the Static pit problem. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to solve the Static pit"! Next, please follow the editor to study!

What would happen without static?

Demand:

1. Define the Student class

Name, nationality, speech behavior

Multiple structures, embodied in the form of overload

2. The nationality of the students is determined.

Nationality can be displayed and initialized.

Public class Student {String name;// name String country;// nationality public Student (String name, String country) {this.name = name; this.country = country;} public void speak () {System.out.println ("name:" + this.name+ "" + "nationality:" + this.country) }} class Test {public static void main (String [] args) {Student student = new Student ("he Daochang", "China"); Student student1 = new Student ("Sun Shuang", "China"); student.speak (); student1.speak ();}}

Running result:

Name: he Jingjing nationality: China

Name: sun Shuangshuang nationality: China

The current problems are:

Now we know that all the students are Chinese, and now every time we create a student object, we have to assign the same value to the nationality attribute of all students, which results in a waste of heap memory space resources.

Current plan:

Move the "China" data to the data sharing area and share the data for use by all Student objects.

Question: how can this data be moved to the data sharing area to share?

Solution:

You just need to modify the data with static

Static member variables maintain only one copy in the data sharing area, while non-static member variables maintain one copy in each object.

Public class Student {String name;// name / / uses static to modify country, then country is a shared data static String country = "China"; / / nationality / / constructor public Student (String name) {this.name = name } / / speech behavior public void speak () {System.out.println ("name:" + this.name+ "" + "nationality:" + country);}} class Test {public static void main (String [] args) {Student student = new Student ("he Daochang"); Student student1 = new Student ("Sun Shuang"); student.speak (); student1.speak () }}

Running result:

Name: he Jingjing nationality: China

Name: sun Shuangshuang nationality: China

Let me explain static in detail.

Static (static modifier)

1.static modifies static variables

If you have data that needs to be shared with all objects, you can use static decorations

How to access static member variables:

Method 1: you can use objects for access

Format: object. Variable name

Method 2: you can use the class name for access

Format: class name. Variable name

Note:

1)。 Non-static member variables can only be accessed using objects, not using class commands.

Public class Student {String name;// name non-static member variable / / uses static to modify country, then country is a shared data static String country = "China"; / / nationality static member variable / / constructor public Student (String name) {this.name = name } / / speech behavior public void speak () {System.out.println ("name:" + this.name+ "" + "nationality:" + country);}} class Test {public static void main (String [] args) {Student student = new Student ("he Daochang"); System.out.println (student.name); / / access non-static variables with objects

Systen.out.println (student.country); / / access static variable System.out.println (Student.country) with object; / / access static variable with class life}

Running result:

He Daojing China

China

2)。 Never use static to modify member variables to facilitate access to data. Use static modification only when the data of member variables really needs to be shared.

Static decorates the application scenario of member variables: if a data needs to be shared by all objects, use static to modify it

2.static modifies member functions (static member methods)

How to access static member functions:

Method 1: you can use objects for access

Format: object. Static function name

Method 2: you can use the class name for access

Format: class name. Static function name

It is recommended to use class names to access static members directly.

Reason:

1. Convenient

two。 Save memory

Things to pay attention to for static functions:

1. Static functions can be called by calling class names or objects, while non-static functions can only be called with objects.

two。 Static functions can access static members, but not non-static members directly.

3. Non-static functions can directly access static and non-static members

4. Static functions cannot have this or super keywords

Public class Student {String name;// name non-static member variable / / uses static to modify country, then country is a shared data static String country = "China"; / / nationality static member variable / / constructor public Student (String name) {this.name = name } / / speech behavior / / static member method public static void speak () {System.out.println ("nationality:" + country);} / / Learning behavior / / non-static member method public void study () {System.out.println (name+ "study hard") }} class Test {public static void main (String [] args) {Student student = new Student ("he Daochang"); System.out.println (student.name); / / access non-static variable System.out.println (student.country) with object; / / access static variable System.out.println (Student.country) with object; / / access static variable student.study () with class life / / access non-static method student.speak () with object; / / access static method Student.speak () with object; / / access static method}} with class name

Running result:

He Daojing

China

China

He Daojing studies hard.

Nationality: China

Nationality: China

The difference between static and non-static member variables:

1) the difference in function:

1. The function of static member variables is to share one data for all objects to use.

two。 The function of non-static member variables is to describe the common properties of a class of things.

2), quantity and storage location:

1. Static member variables are in the memory of the storage method area, and only one piece of data exists.

two。 Non-static member variables are stored in heap memory, and there are n data for n objects.

3), the difference of life cycle:

1. Static member variable data exists when the class is loaded and disappears with the disappearance of the class file.

two。 Non-static member variable data exists with the creation of the object and disappears as the object is reclaimed by the garbage collector

Static functions cannot access non-static members?

Static functions can also access non-static data as long as there are objects, but not directly.

Finally, continue to use this example to interspersed the knowledge of static code blocks.

Static code blocks are executed as soon as the Student.class file is loaded into memory

Public class Student {String name;// name non-static member variable / / uses static to modify country, then country is a shared data static String country = "China"; / / nationality static member variable / / static code block static {System.out.println ("static code block is executed!") ;} / / Constructor public Student (String name) {this.name = name;} / / speech behavior / / static member method public static void speak () {System.out.println ("nationality:" + country) } / / Learning behavior / / non-static member method public void study () {System.out.println (name+ "learn well");}} class Test {public static void main (String [] args) {Student.speak ();}}

Running result:

Static code block executed!

Nationality: China

When you understand this, if you look at the following analysis chart, you should still get something.

At this point, the study on "how to solve the Static pit" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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