Why can't Java-static methods call non-static methods

introduction

We all know that a non-static method cannot be called in a static method. To be precise, a non-static method cannot be called directly. But you can pass the reference of an object into the static method, and then call the non-static method of the object.
In fact, the application of this fact is so often that we don't pay attention to it: in the main function (static method) we often create an instance of a certain class, and then use its reference variable to call its non-static method.

class Test2{
    
    
	public void method2(){
    
    
		System.out.println("HelloWorld2");
	}
}
 
class test{
    
    
	public void method0(){
    
    
		System.out.println("HelloWorld0");
	}
	public static void method1(){
    
    
		System.out.println("HelloWorld1");
	}
	
	public static void main(String args[]){
    
    			
		new test().method0(); 
		method1(); 
		new Test2().method2(); 
	}	
}

打印结果:
HelloWorld0
HelloWorld1
HelloWorld2

Why can't static methods in java call non-static methods?

This should be analyzed from the memory mechanism of Java. First, when you New an object, you do not first open up memory space for the object in the heap, but first use the static methods (static functions with static modification) in the class. The code is loaded into a place called the method area, and then the object is created in the heap memory. So static methods will be loaded as the class is loaded. When you new an object, the object exists in the heap memory. The this keyword generally refers to the object, but if there is no new object, you can call the static method of the class through the class name.

Programs are ultimately executed in memory, and variables can only be accessed when they occupy a place in memory.

1. The static members (static and method) of the class belong to the class itself, memory will be allocated when the class is loaded, and can be directly accessed through the class name.

2. Non-static members (variables and methods) belong to the object of the class, so memory is allocated only when the object of the class is created (instance is created), and then accessed through the object of the class.

The reason why an error occurs when accessing a non-static member in a static member of a class is because the static member already exists when the non-static member of the class does not exist. Of course, an error will occur when accessing a non-existent thing in memory.

If the static method can access the non-static method: First, the static method is that when you new an object, you first put the static method into the method area in the heap along with the class file. Only then will an object be created in the heap. In other words, the static method already exists in the class, and the non-static method has not yet been created. If the non-static method called by the static method has not been created, an error will be reported.

The JVM will definitely not take this risk and let you call a method that may not exist, so simply report an error when you call it to avoid unnecessary trouble in the future.

** The modified member variable of the static keyword, the member method has nothing to do with the object.

The content of static modification is loaded with the loading of the class, and only loaded once.

It is stored in a fixed memory area (static area), so it can be called directly by the class name.
It exists before the object, so it can be shared by all objects.

Static can be used to modify member variables and member methods. The modified member belongs to the class, not just
to an object. In other words, since it belongs to a class, it can be called without creating an object, but can be called directly using the class name.

to sum up:

First of all static members are initialized when the class is loaded, the JVM of CLASSLOADER loaded for the first time take the initiative to use load instead of static members is when creating the object, that new operation when it is initialized;
the order is loaded first , Then initialize static members when loading. At this time, non-static members must not be used before being loaded. Non-static members are initialized when the object is created through the new operator after the class is loaded. static has allocated memory space, so it can be accessed!
Simply put: static members belong to the class and exist without generating objects. Non-static members need to be generated to generate objects. Therefore, static members cannot directly access non-static members.

Let's talk about static characteristics:

1. Load as the class loads

That is to say, static will disappear with the disappearance of the class, indicating that the static life cycle is the longest

2. Prioritize the existence of the object

To be clear: static is the object that exists first and then exists

3. Shared by all objects

4. Can be directly called by the class name

The difference between instance variables and class variables

1. Storage location

Class variables exist in the method area as the class is loaded, and instance variables exist in the heap memory as the object of the object is created.

2. Life cycle

Class variables have the longest life cycle. They are loaded as the "class" is loaded, disappear as the class disappears, and instance variables disappear as the "object" disappears.

Note for static use:

1. Static methods can only access static members (including member variables and member methods)

Non-static methods can access static or non-static

2. This and super keywords cannot be defined in static methods

Because static takes precedence over objects, this and super keywords cannot appear in static methods

3. The main function is static.

Static pros and cons

Pros: The shared data of the object is stored in a separate space, saving space. It is not necessary to store a copy in each object, which can be directly called by the class name

Disadvantages: The life cycle is too long, and the access is limited (only static)


Continuously updating...
Article reference learning address: https://blog.csdn.net/jiayi_yao/article/details/51346378
Article reference learning address: https://blog.csdn.net/qq_35499112/article/details/84886871

Guess you like

Origin blog.csdn.net/weixin_44325444/article/details/109273226