Java Learning notes: Java internal classes

Source: Internet
Author: User

First, Introduction

Inner classes: Classes that exist with classes are internal classes that are typically used for Android development.
The inner class can be understood as an inheritance relationship
1. Ordinary Inner class
2. Local inner class
3. Static Inner class
4. Anonymous inner class

Second, notes

1. Ordinary Inner class

/*** Ordinary inner class: class declared in class, outside of method * 1. The inner class can directly access the properties and methods of the outer class containing the private * 2. External classes cannot directly access the properties and methods of an inner class, can be accessed through an object, and contain a private * **/ Public classNormalinnerclass { Public Static voidMain (string[] args) {Normalinnerclass.son Fson=NewNormalinnerclass ().NewSon (); }    PrivateString namestring;  Public voidTell () {}//ordinary inner classes can be understood as a special way to create objects    classSon {Private intAge ;  Public voidTell () {System.err.println (namestring); }    }}

2. Local inner class

 /**   * local Inner class: A class declared in a method whose scope can only be in the method that declares the class * 1. Local inner classes, which can access all methods of the outer class directly, including private * 2. His scope can only be used in methods that declare this class, other methods cannot be accessed, and other classes cannot access * 3. The local inner class * 4 is accessed through an external inner class. Ordinary inner classes cannot have static and static variables *  */ public    Localinnerclass { private     String namestring= "admin" ;  public  void   tell () { class   son{ Span style= "color: #0000ff;"                            >public  void   tell () { }        }    }}

3. Static Inner class

/*** Static Inner class: * 1. Inner classes can only access static properties and methods of external classes directly. * 2. External classes can access only the static properties and methods of the inner class amount directly through the class name. The member properties and methods need to be created by creating an object * 3. You can have static methods static variable instance variable instance method * 4. Other classes can call static properties and methods directly through the class name, by creating objects to visit common methods **/ Public classStaticinnerclass {Private StaticString name; Static classson{PrivateString tr; Private Static intAge ;  Public voidTell () {System.out.println (name); }            }}

4. Anonymous inner class

/*** Anonymous inner class by calling * **/ Public classAnonymityinnerclass { Public Static voidMain (string[] args) {Father Father=NewFather () {@Override Public voidMenth () {//TODO Auto-generated method stubs                Super. Menth ();    }        }; }}classFather { PublicFather () {//TODO Auto-generated constructor stub    }     Public voidMenth () {System.out.println ("1"); }}

Java Learning notes: Java internal classes

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.