JAVA internal class

Source: Internet
Author: User

JAVA internal class

1. Common internal classes (internal classes are not static)

The internal class can access all the members of the peripheral object. When an internal class object is created for a peripheral class object, the internal class object will capture a reference pointing to the peripheral class. When accessing the members of the peripheral class, the reference is used to select the members of the peripheral class. If the compiler cannot access this reference, an error is returned.

Common internal classes cannot have static attributes and methods. If so, nested classes must be used.

 

Class Outer {public class Inner {} Inner in = new Inner (); // reference to this external object is this} public class Test {public static void main (String [] args) {// Outer outer = new Outer (); // Outer. inner in = outer. new Inner (); // these two sentences are exactly equivalent to Outer. inner in = new Outer (). new Inner ();}}

 

Note: Outer. inner in = new Outer (). new Inner (); when importing a package to a specific internal class, you can write it as Inner in = new Outer (). new Inner (); otherwise, the external class must be taken. When creating a new object, you must first obtain the reference of the peripheral class object.

2. Anonymous internal class

The following example combines the generation and definition of the returned value type with no name. When creating object A, object A is actually an interface and cannot be new directly. Therefore, an unimplemented method must be implemented, in fact, the class object that implements an interface is then transformed to a reference of the parent class.

If you want to use an external defined object within an anonymous internal class, the reference of this parameter must be final.

interface A{public void method();}class AImp implements A{public void method(){System.out.println("AImp");}}class Test{private static A getA(){//return new A() {//public void method() {//System.out.println("Aimp_Test");//}//};return new AImp();}public static void main(String[] args) {getA().method();;}}

3. Nested classes

If you do not need to associate an internal class object with an external class object, you can declare the internal class as static. Different Internal class objects implicitly save a reference pointing to objects outside of them. Nested classes do not need peripheral class objects when creating objects, but they cannot access non-static attributes and methods in the peripheral classes.

The broadcast receiver in Android requires a static nested class when using static declaration. Otherwise, the class cannot be found.

class Outer{private int x;public static class Inner{//int y = x;// error}Inner in = new Inner();}public class Test{public static void main(String[] args) {Outer.Inner in = new Outer.Inner();}}

When Handler is used in Android as follows, the ThisHandler class shocould be static or leaks might occur will be warned. The Handler class should be static; otherwise, the memory will be leaked because the internal class MyHandler is created, A reference to the External Department class is also retained, generally the Activity. Once the activity is destroyed, a problem may occur.

class MyHandler extends Handler{public void handleMessage(android.os.Message msg) {switch (msg.what) {}};};private Handler handler = new MyHandler();

4. Internal class transition to interface-expose/hide partial implementation

class B{private void f(){System.out.println("f()");}private void g(){System.out.println("g()");}public class AImp{public void method1(){f();}public void method2(){g();}}}public class Test{public static void main(String[] args) {B b = new B();AImp aImp = b.new AImp();aImp.method1();aImp.method2();}}

Since AImp is public, you can access it and access all its methods. The following interface is used to expose only the method1 function. To transform the interface to an interface, you can put the function you want to expose in the interface.

Interface A {void method1 ();} class B {private void f () {System. out. println ("f ()");} private void g () {System. out. println ("g ()");} public A getA () {return new AImp ();} private class AImp implements A {public void method1 () {f ();} public void method2 () {g () ;}} public class Test {public static void main (String [] args) {B B B = new B (); // AImp aImp = B. new AImp (); // Since AImp is private, errorA aImp = B. getA (); // B. getA () To transform to an interface, you can put the functions you want to expose in the interface. AImp. method1 (); // only expose menthod1 // aImp. method2 (); // The method method2 () is undefined for the type }}







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.