Some problems in the early stage of Javase

Source: Internet
Author: User

??? For the following question 1: Compile phase Demo1 will error, DEMO2 will not error.

Class demo1{

int i;

i = 0;


Class demo2{

int i = 0;


In fact, in Java, you define a class in which a class can only contain member variable member methods to construct method code blocks;

The member variable is the property of the class, the member method is the behavior of the class, and the constructor is used to create an instance of the class;

Code blocks include static code blocks and non-static code blocks:

The static modified code block is loaded with the load of the class, generally as the driver of the class;

Code blocks that do not have static adornments are constructed as blocks of code and executed before the method is constructed;

As a result, you can see that the member position in the class can only be defined, defining the attribute characteristics of a class, and if the member bit

It is illogical to call a method or execute a statement, which is equivalent to letting the class implement an action.

You must create an instance of the class, invoke the method through an instance, and implement the action in the method.

Class demo3{

int i;





DEMO3 does not error, i=0 in the construction code block, this class is the same as the following class:

Class demo4{

int i;

Demo4 () {




This is equivalent to executing the i=0 statement in the Demo4 constructor, and ultimately by invoking the method.

Question 2: Why static cannot modify local variables, only member variables and methods can be decorated, and may be called directly with the class name.

The static modified member variable or member method is loaded as the class loads.

In Java, when a class is loaded by a virtual machine, it produces a class object of type classes that exists in the

The method area of the JVM, which is the class file that is generated. When creating instances of this class, this class object will be used as a template

, create a space in the heap memory and copy the member variables of the class object, and there will be a reference to the class object

The member method is executed by referencing the method call in the class object when the method is called.

A member variable or method that is modified with static is actually a class object, that is, the template is not

An instance of a class. Then the member variable or method that calls the static decoration can use the class name directly to point to the

Variables and methods, of course, can also be called by creating objects.

Why static can not be modified local variables, first local variables exist in the method, its scope in this

method, and the invocation of the method relies on the creation of the object, so the local variable exists in the heap memory, and the

Static modified variables are always present in the method area as the classes are loaded, and the life cycle and location are different, so

You cannot modify a local variable with static.????

Some problems in the early stage of Javase

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.