java-static learning

       In java, the static keyword can also modify a class - this is an inner class. It is also only possible to decorate inner classes.

       First, define the outer class and the static inner class

	static int outerField = 0;
	static int b(){
		return 1;
		
	}
	int out;
	staticTest(){
		
	}
	static class inner{
		staticTest bb = new staticTest();
		int s = b();
		void InnerMethod(){
            int i = outerField;
        }
		// error below
		int in = out;
	}

      As can be seen from the code, the static inner class can access the static members of the outer class, but cannot access the non-static members of the outer class.

 

       access static inner class

public static void main(String[] args){
		staticTest t = new staticTest();
		staticTest.inner n = new staticTest.inner();
		//The following code is an error The method inner() is undefined for the type staticTest
		n = t.inner();
	}

      As you can see, a static inner class can be accessed through the outer class, but not through an instance of the outer class. You can even access static inner classes in the form of import

import codeTest.staticTest.inner;

         According to yesterday's study, it is easy to think that a static inner class is actually just a static member of a class. It follows the entire class and cannot be accessed through any instantiated outer class, and does not follow any outer class objects that must be instantiated. generate relationship. Static members and non-static members can be defined in static inner classes.

         execute main function

public static void main(String[] args){
		staticTest t = new staticTest();
		staticTest.inner n = new staticTest.inner();
		//The following code is an error The method inner() is undefined for the type staticTest
//		n = t.inner();
		staticTest.inner n2 = new staticTest.inner();
		n.s = 2;
		System.out.println(n.s);
		System.out.println(n2.s);
	}

     result

2
1

     

Note that although the inner class is modified by the static keyword, two copies are actually reserved in memory.

      For non-static inner classes, the situation is a little more complicated. . First, inner classes cannot have static member variables. But for basic data types and strings, there can be static final variables.

        There are a lot of answers on the Internet, but I can't understand a lot of them. . Personally, I think this is actually what the java designers came for convenience. There are many such designs in java. If the original design of java allows static inner classes to have static variables, is that okay? of course can. But this doesn't seem to be necessary, and it may cause great inconvenience to code writing.

        Of course, if you think this doesn't convince you, there is another explanation: for a non-static inner class, to access it, there must be an instantiated outer class, let's say there are two outer classes, A and B, in There are two blocks of memory in the memory. Use them to access non-static inner classes respectively, and non-static inner classes will definitely occupy a position in each of the two pieces of memory. After all, he is two non-static members of two instance objects. So, if there is a static member in the inner class at this time, does the static member belong to A or B? Paradoxically, the java designers forbid this situation. .

         There is another explanation, the problem of jvm loading order. . When you want to see it, search it online. This explanation is the most common. . .

         As for strings and various basic types, my personal guess is that they are stored in the constant pool of external classes. There are many online sayings, and there is no official document. I hope God can popularize it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326241699&siteId=291194637