Java Internal classes

Source: Internet
Author: User
Tags modifier

What is an internal class?

Class that is placed inside a class, which we call the inner class

Why use internal classes?

1. Internal classes provide a better encapsulation, and only external classes can access the inner classes directly.

2. Internal classes can inherit and implement interfaces without the influence of external classes.

3. The methods and properties of the inner class are not directly accessible even for external classes, while internal classes can access the properties and methods of external classes at any time.

4. Facilitates the writing of callback functions

Java internal classes are divided into 4 types

member Inner class

Method (local) inner class

Anonymous inner class

Static Inner class

How to use

member Inner class

member Inner class 1. Cannot have static modified variables and methods, if there is a need to add final decoration 2. The member inner class is dependent on the outer class, and only the external class is created to create the inner class 3. The external class cannot directly access the properties and methods of the inner class and requires an instance of an inner class to access the *  * @author Administrator * */public class Outer {private int num=5; class inner{private int num=10;public void print () {//Call inner class variable System.out.println ("inner class Method" +num);// Call External class variable SYSTEM.OUT.PRINTLN ("External class method" +outer.this.num);}} public void print () {SYSTEM.OUT.PRINTLN ("external class method"); public static void Main (string[] args) {
Mode one//Create external class object Outer out=new Outer ();//Create inner class object Inner inner = out.new inner (); Inner.print ();//External class method Out.print ();
Mode two//Create inner class object Outer.Inner in=new Outer (). New Inner ();//Call inner class method In.print ();}}

  

Method (local) inner class

/** * Method Inner class * 1. Cannot have static variable * 2. You can only invoke constants within a method * 3. Cannot have access modifier * @author Administrator * */public class Outer1 {public void print () { final int nums=5;class inner{private int num=10;public void print () {System.out.println ("inner class method variable" +this.num);// Call the method inside the variable System.out.println ("outer class method variable" +nums);}} Call the inner class method new Inner (). print ();} public static void Main (string[] args) {new Outer1 (). print ();}}

  

Anonymous inner class

/** * Anonymous Inner class * 1. Must inherit an abstract class or interface * 2. Cannot have static modified member variables and methods, only final decoration * 3. The variables within the method must use the final decoration * 4. There is no access modifier * 5. No construction, because he has no class name * @au  Thor Administrator * */public class Outer2 {private int num=5;public void print () {final int num=10;new Inner () {public void Print () {SYSTEM.OUT.PRINTLN ("external class" +outer2.this.num); System.out.println ("inner class" +num);}}. Print ();} Define an Interface interface inner{void print ();} public static void Main (string[] args) {new Outer2 (). print ();}}

  

Static Inner class

/** * Static Inner class * 1. You do not need to rely on external classes, you can create the * 2 directly. Static inner classes cannot use non-static members and methods of external classes, while inner classes can be * 3. The   external class name cannot be used. This. Property name/method * @author Administrator * * /public class Outer3 {private static int nums=5;//creates static internal class static class Inner{private int num=10;public void print () {System. Out.println ("inner class" +num); SYSTEM.OUT.PRINTLN ("External class" +nums);}} public static void Main (string[] args) {Inner in=new Inner (); In.print ();}}

  

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.