On the static and dynamic problems of Java
Cannot make a static reference to the non-static field
Non static member variables and methods cannot be referenced in static methods
Because the instance variable should be the state of a specific object, a = new a(); Then system out. println(a,a);
Or change the variable to static
No enclosing instance of type EventApp4 is accessible. Must qualify the allocation with an enclosing instance of type EventApp4 (e.g. x.new A() where x is an instance of EventApp4).
Reason: the inner class is not limited by static, so it is dynamic, and I new this inner class in the main function. Why is this a problem? Because static methods and variables have entered memory when the class is loaded, but non-static methods and variables can only enter memory after instantiation. Therefore, new non-static internal class in static methods will make an error, because this internal class does not exist at this time. On the contrary, in dynamic methods, new static methods will not make errors. The same reason is that static methods already exist before non-static methods.
From the memory mechanism of Java, first of all, when you create an object, you don't first open up memory space for the object in the heap, Instead, the static methods in the class are The code of (static function decorated with static) is loaded into a place called the method area, and then the object is created in the heap memory. Therefore, the static method will be loaded with the loading of the class. When you new an object, the object exists in the memory. The this key generally refers to the object, but if there is no new object, you call the static function of the class through the class name The method is also OK.
In case of problems, there are the following principles
1. In main, use static methods and static classes, that is, variables and methods are limited to static
2. In main, instantiate this class or other classes (classes to be used)
3. Put the statement in main into the constructor and instantiate this class in main, because this class can be used in the constructor
The above detailed discussion on the static and dynamic problems of Java is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.