Error cannot reference non-static variables from Static Context

Source: Internet
Author: User
Error cannot reference non-static variables from Static Context 20:58:52

Category: Java

 

What is "static "?

Anyone who has learned Java, C ++, or C should understand this keyword. The variable modified with this keyword is called a static variable, which has a special effect. In Java, static is also used to modify static methods and static internal classes.

 

Static variables:

(1) life cycle: the life cycle of static local variables is also the entire source program. When the function defining the entire variable ends, the entire variable does not disappear and it always exists.

(2) scope: the scope is the same as that of the automatic variable. It can only be used in the defined subfunctions. When the function is exited, the entire variable continues to exist, you can no longer use it.

(3) If no initial value is assigned to the entire static variable, the default value is 0.

(4) In the object-oriented programming language Java, static variables can be directly used without instantiating objects. (Note: This feature is especially suitable for defining constants, similar to the macro definition in C language. Example: public static final double Pi = 3.14 ;)

 

Static method features:

(Note: The static method here can also be called a static function)

(1) methods (functions) that can be directly used without instantiating objects ).

(2) This method can only access static member attributes in this class.

(3) in Java, if you want to access dynamic member attributes in static methods, you must first instantiate them in this method and then access them through specific objects.

Summary: In the static method, data that already exists in the memory space is accessed, such as static data, which is irrelevant to the object. As long as this class is used, it exists, no Initialization is required. All objects share one copy. Dynamic members must be instantiated, and each object has a copy with different memory addresses. Therefore, they must be instantiated before static methods can access dynamic resources.

 

When do I need to use static domains (static attributes) and static methods?

(1) If a method runs at project startup, the static method must be used at this time. The most common example is the main method. This method starts to run before it is instantiated, so it must be a static method.

(2) For macro definition of a constant like in C, static variables are required.

(3) When a variable is shared in multiple places, it is best to use static variables.

(4) When a method is to be used repeatedly, it is best to use the static method.

 

The following is an example:

 

Import java. util .*;

 

Public Class {

Final string name = "first ";

Public static final double Pi = 3.14;

Public static void main (string [] ARGs ){

// A = new ();

System. Out. println ("it is OK! ");

System. Out. println (name );

}

Void AA (){

System. Out. println ("I Am .");

System. Out. println (name );

}

}

This program reports an error. The error is "non-static variables cannot be referenced from a static context". You can see at a glance that dynamic data members are accessed in static void main. We know that classes must be initialized when defining classes, but why? During initialization, the dynamic attribute members in the class are initialized and assigned values.

So how can we modify the above errors? With the above ideas, we can imagine if we can instantiate this class, we can access this dynamic attribute through the object.

 

 

Import java. util .*;

 

Public Class {

Final string name = "first ";

Public static final double Pi = 3.14;

Public static void main (string [] ARGs ){

A A = new ();

System. Out. println ("it is OK! ");

System. Out. println (A. Name );

A. AA ();

}

Public static void AA (){

System. Out. println ("I Am .");

System. Out. println (PI); // This pi is a static member attribute and can be accessed directly.

}

}

 

 

 

We can find that the above problem has been solved.

Summary: static attributes can be directly accessed in static methods. dynamic attributes must be instantiated and accessed through specific objects.

Error cannot reference non-static variables from Static Context

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: info-contact@alibabacloud.com 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.