java-inner class learning

        Inner class is a special class in java. Create a class inside a class.

package codeTest;

public class Outer {
	int out_a = 1;
	private class inner{
		private int inner_a = out_a;
	}
//	int out_b = inner_a;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Outer o = new Outer();
		Outer.inner i = o.new inner();
		int j = i.inner_a;
		// TODO Auto-generated method stub

	}

}

         To access the inner class, use the method of the outer class .new inner class constructor. The above code does not report an error, indicating that the members of the outer class can be freely accessed in the inner class, and when the inner class is instantiated in the outer class, all members of the inner class, including private members, can be accessed.

        Check the compiled file and find that the inner class is compiled into Outer$inner.class. Use the decompilation tool to view the class file. The results are as follows:

package codeTest;

class Outer$inner
{
  private int inner_a;

  private Outer$inner(Outer paramOuter)
  {
    this.inner_a = paramOuter.out_a;
  }
}

          It means that when the JVM compiles the inner class, it will create a constructor with the outer class object as a parameter by default. Modify inner class constructor

class inner {
		public inner(int i) {
		}
		public inner(int... i) {
		}
		public inner(int i,String s) {
		}
                public inner(int i,String s,Outer o) {
		}
public int inttest(){
			int i = out_a;
			return i;
		}

	}

    Decompile the class of the inner class:

package codeTest;

class Outer$inner
{
  public Outer$inner(Outer paramOuter, int i)
  {
  }

  public Outer$inner(Outer paramOuter, int[] i)
  {
  }

  public Outer$inner(Outer paramOuter, int i, String s)
  {
  }

  public Outer$inner(Outer paramOuter1, int i, String s, Outer o)
  {
  }
 public int inttest()
  {
    int i = this.this$0.out_a;
    return i;
  }
}

         Explain that no matter what parameters your inner class constructor uses, it will add a new outer class to the parameter list. This explains why the inner class can access the members of the outer class: the members of the outer class are initialized into the inner class in the constructor.

       Since I started a project, the most common inner class I encountered was anonymous inner class. The most encountered are list custom sorting and new threads:

	public static void main(String[] args) {
		List<Integer> l = Arrays.asList(1,8,4,14,25,5,88,2);
		Collections.sort(l, new Comparator<Integer>(){
			@Override
			public int compare(Integer o1, Integer o2) {
				// TODO Auto-generated method stub
				return o1 - o2;
			}
		});
		System.out.println(Arrays.toString(l.toArray()));
	}

 result:

[1, 2, 4, 5, 8, 14, 25, 88]

 Thread:

public static void main(String[] args) {
		new Thread(){
			public void run(){
				System.out.println("My Thread");
			}
		}.start();
	}

 

   result:

My Thread

     As for other cases, why use inner classes, most of the answers on thinking in java and online are to hide the concrete implementation, and to enable multiple inheritance of classes. So far, I haven't found any need in this regard. It may be that I have written too few programs. Mark it first, and then come back to modify the blog if it is useful in the future.

Guess you like

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