Home Java javaTutorial Java static and dynamic problem solving

Java static and dynamic problem solving

Sep 19, 2017 am 11:42 AM
java solve question

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


1

2

3

4

5

6

7

class A {

  private int a = 1;

 

  public sttaic void main(String[] args){

    System.out.println(a);

  }

}

Copy after login

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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Square Root in Java

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Perfect Number in Java

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Random Number Generator in Java

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Armstrong Number in Java

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Weka in Java

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

Java Spring Interview Questions

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Break or return from Java 8 stream forEach?

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Smith Number in Java

See all articles