Internal class access to external class variables must be final, non-static variables cannot be referenced in Java static methods, instances of inner classes cannot be created in static methods

Source: Internet
Author: User

must the internal class access the variables of the external class be final ?as Follows:
Class A
{
int i = 3;
public void Shout ()
{

Class B
{
public void Shout1 ()
{
System.out.println (i);
}

}
b b=new B ();
B.SHOUT1 ();
}

public static void main (String [] Args)
{
A a=new a ();
A.shout ();
}
}
The normal output is 3, proving that you can access the Class's variable i, but change to the following way:
Class A
{
public void Shout (int Temp)
{
final int i = temp;
Class B
{
public void Shout1 ()
{
System.out.println (i);
}

}
b b=new B ();
B.SHOUT1 ();
}

public static void main (String [] Args)
{
A a=new a ();
A.shout (3);
}
} At this point I must be final, how to understand it? The collection  Best Answer 
yes, because of the life Cycle. A local variable in the method, which is released when the method ends, and final guarantees that the variable always points to an object.

first, the inner class and the outer class are actually at the same level, and the inner class is not destroyed because the definition in the method will follow the execution of the Method. The problem is that if the variable in the method of the outer class does not define final, then when the outer class method executes, The local variable must be gc, but a method of the inner class has not been executed, and the external variable that he refers to cannot be found. If defined as final,java, this variable is copied as a member variable inside the inner class, so that the memory area that the variable points to will not change because the value that is modified by final is always immutable.

non-static variables cannot be referenced in Java static methodsBecause we know that a static method can be used without creating an instance, and a member variable declared as nonstatic is an object property that is referenced only when an object exists, so if we call a Non-static member method in a static method when the object is not created, the compiler will give the error at this Time.

Simply put, a static method can be called without creating an object, and a non-static method must have an instance of the object in order to be called. so it is not possible to refer to a non-static method in a static method, because what object does it refer to as a non-static method? the compiler cannot give an answer because there is no object

Class HelloWorld

{
int a1 = 6;

public static void main (string[] Args)
{

System.out.print (a1);
/** member variables cannot be called directly (non-static variables cannot be referenced from a static context A1)
*/
}
}

An instance of an inner class cannot be created in a static method

As shown, Outter is an ordinary class, inner is inside the outter class, and belongs to the inner class. In the main method to try to create an instance of inner, MyEclipse will have an error prompt, to the effect that: "no outter can access the instance, you must specify an instance of the Outter class (such as: x.new a () where x is an instance of the Outter class)"

Reason:

The inner class is characterized by the ability to access member variables of an external class, and member variables can only be accessed after an instance has been Created.

Then the main method is a static method, that is, you can access this method without creating an instance of outter, and if inner can create an instance, the inner instance may access the member variables of the outter object.

But the main method is that static methods can run without creating objects, Outter objects are not created, and inner can not access member variables of Outter objects even if they are able to create Objects.

therefore, You cannot create an object of an inner class in a static method, preventing the inner class object from accessing the member variables of the outer class before the Outer class object is Created.

Internal class access to external class variables must be final, non-static variables cannot be referenced in Java static methods, instances of inner classes cannot be created in static methods

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.