Java Internal classes

Source: Internet
Author: User

Access rules for internal classes:
1, the inner class can access the members of the external class directly, including the private. Members that are modified by private can only be used in the class in which they are defined and cannot be used in other classes.
Members in an external class can be accessed directly because the inner class holds a reference in an external class, in the format: external class name. this. Example: Outer.this.num
2, the outer class must first establish an inner class object to access the inner class.

Access format:
1, when an inner class is defined at the member location of an external class and is not private, it can be accessed in other classes outside the class, and can be accessed directly by building an inner class object.
Format: outer class name. Internal class Name Variable name = External Class object. Inner Class object
Outer.Inner in=new Outer (). New Inner ();
2, when the inner class is in the member position, it can be decorated by the member modifier.
For example: Private: The inner class is encapsulated in the outer class.
Static: The inner class has static properties.
When an inner class is modified by static, only static members in the outer class can be accessed directly, and access restrictions occur.

In other external classes, how do you directly access the non-static members of the static inner class?
New Outer.Inner (). function ();//need to create an inner class object
Format:
External class. Inner class variable =new external class. Inner class ();
Variable. function ();

In other external classes, how do you directly access static members of static internal classes?
Outer.Inner.function ();

Note: When a static member is defined in an inner class, the inner class must also be static.
When a static method in an external class accesses an inner class, the inner class must also be static.

Static can be accessed statically, cannot access non-static;
Non-static can be accessed statically, or it can be accessed non-static.


When describing things, there is something inside the thing, which is described by an inner class.
Because internal things are using the content of external things.

There are static methods in the inner class, so the class must also be static.
There are abstract members in a class, then this class must also be an abstract class.

The inner class appears at the location of the local member, not the member's location. The inner class at this time cannot be decorated with static.
Inner classes as members can be decorated with private, static
An inner class that is a local member (that is, an inner class inside a member function) cannot be decorated with private and static.
A local inner class cannot define a static member, because if there is a static member in the local inner class, the local inner class must also be static, and the local inner class cannot be static, so the static members are not included in the local inner class.

1, Anonymous inner class: The simplified form of the ordinary inner class.
2, the premise of an anonymous inner class: the anonymous inner class inherits an external class or implements an interface.
3, Anonymous inner class format: New parent class or interface () {Defines the contents of a subclass}

4, anonymous The inner class is actually an anonymous subclass object. And the object is a little fat. Can be understood as an object with content.

Interface members in the inter//interface are constants (public static final ... ) and abstract methods (public abstract). The members in the interface are public, so when the class implementing the interface overrides the interface member, the modifier must be public. {public static final int x=9;void method ();//is actually public abstract void method ();} Class test{static Inter function () {return new Inter () {public void method () {System.out.println ("haha" +x);}};}} class Innerclasstest{public static void Main (String args[]) {test.function (). method ();/* Parse the previous line of code: Test.function (): The Description class test has a static method function (). A static method in a class can be called directly with the class name. static method Name (). For non-static methods, you need to create an object to invoke.    . Method (): Describes invoking method methods in an interface, but we know that only the class object that implements the method interface can invoke the methods method. So the Test.function () method inevitably returns an object of type Inter. */}}


Java Internal classes

Related Article

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.