In Java (vii): internal class __java

Source: Internet
Author: User

1. Internal class access to the member variables of the external class: Outer.this.size

public class Outer {
	private int size;

	public class Inner1 {
		private int size;
		public void display (int size) {
			size++;//refers to the form parameter this.size++ of the display function
			;//refers to the member variable
			in the inner class outer.this.size++; The member variable in the outer class is referenced
		}}}
2. How internal classes are referenced externally: outer.inner Inner = outer.new Inner ()

To create an instance object of an inner class, you must first create an instance object of an external class

public class Testinner {public
	static void Main (string[] args) {
		Outer Outer = new Outer ();
		Outer.Inner Inner = Outer.new Inner ();
		Inner.display ();
	}

Class outer{
	private int size = ten;
	public class inner{public
		void display () {
			System.out.println (size);
		}
	}
}
3. Internal classes defined in a method if you want to access local variables in a method, the local variable must be declared final
public class Outer {public
	void Test () {
		final int x = 0;
		Class Inner {
			void display () {
				System.out.println (x);
			}
}}}

4. External classes can directly access static inner classes without creating external class objects

Class Outer {
	static class inner{
		
	}
} public

class test{public
	static void Main (string[] args) {
		Outer.Inner Inner = new Outer.Inner ();
	}
}



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.