Home  >  Article  >  Java  >  Java static and dynamic problem solving

Java static and dynamic problem solving

黄舟
黄舟Original
2017-09-19 11:42:181665browse

The following editor will bring you an article that discusses the static and dynamic issues of Java in detail. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

Cannot make a static reference to the non-static field

Static methods cannot reference non-static member variables and methods


class A {
  private int a = 1;

  public sttaic void main(String[] args){
    System.out.println(a);
  }
}

Because the instance variable should be the state of the specific object, you should first A a = new A(); and 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 internal class is not qualified with static, so it is dynamic, and I new this internal class in the main function. Why does this cause problems? Because static methods and variables have already entered the memory when the class is loaded, but non-static methods and variables can only enter the memory after instantiation, so in the static method, new is not A static inner class will cause an error because the inner class does not exist yet. On the contrary, there will be no error in the new static method in the dynamic method. For the same reason, the static method already exists before the non-static method.

Analyzing from Java's memory mechanism, first of all, when you New an object, you do not first open up memory space for the object in the heap, but first add the static method in the class (with static modification Static function) code is loaded into a place called the method area, and then the object is created in the heap memory. So the static method will be loaded as the class is loaded. When you new an object, the object exists in the memory. The this keyword generally refers to the object. However, if there is no new object, you can also call the static method of the class through the class name.

When encountering problems, there are the following principles

#1. In main, use static methods, static variables, and static classes, that is, variables , methods are limited to static

2. In main, instantiate this class or other classes (the class to be used)

3. Put the statements in main into the constructor, Instantiate this class in main, because the reference of this class can be used in the constructor this

The above is the detailed content of Java static and dynamic problem solving. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn