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

Why java static methods can't call non-static methods and variables

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains why java static methods can't call non-static methods and variables. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn why java static methods can't call non-static methods and variables.

First, examples

Let's first look at the effect:

When we call a non-static variable or a method in a static method main, an error is reported. Let's look at it the other way around:

There is no problem on the contrary, so let's explain why:

II. Explanation of the reasons

The first thing we need to know is that static methods and static variables belong to a class, not to a class. Let's not talk about the reason directly, let's start with jvm:

This is a lifecycle diagram of class loading.

1. Load

"loading" is the first process of the "class addition mechanism". In the loading phase, the virtual machine mainly accomplishes three things:

(1) obtain the binary byte stream defined by a class through its fully qualified name

(2) convert the static storage structure represented by this byte stream into the run-time data structure of the method area.

(3) generate a Class object representing this class in the heap as an access entry to the data in the method area.

Note that static data structures such as static variables or static methods are scanned in our code at this time, and memory has not yet been allocated.

2. Verification

The main function of validation is to ensure the correctness of the loaded class.

3. Preparation

The preparation phase mainly allocates memory for class variables and sets initial values. This memory is allocated in the method area. Note that memory is allocated for our class variables, that is, static variables, but not for ordinary member variables.

4. Analysis

The parsing phase is mainly the process that the virtual machine converts the symbolic references in the constant pool into direct references.

5. Initialize

This is the last step in the class loading mechanism, and it is at this stage that the java program code begins to execute. We know that the class variable has been assigned a value once in the preparation phase. At the initialization stage, programmers can assign values according to their own needs. Values are assigned to our ordinary member variables only at initialization time.

Writing that the answer has come out, static methods belong to the class, dynamic methods belong to the instance object, memory is allocated when the class is loaded, and can be accessed directly through the class name. Non-static members (variables and methods) belong to the object of the class, so it exists only after the object is initialized, and then accessed through the object of the class.

That is, if we call a non-static member variable in a static method, we will be ahead of time, and we may call an uninitialized variable. So the compiler will report an error.

At this point, I believe you have a deeper understanding of "why java static methods can not call non-static methods and variables". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report