A little discussion of Java anonymous inner classes

Source: Internet
Author: User
Tags anonymous instance method int size thread
Basic theory:

About Java Inner classes: an inner class is defined as a class that is defined within another class.
The reason for this exists is:
1. An object of an internal class can access the implementation of the object that created it, including private data. That is, an internal class instance is privileged for an instance of which class contains it.
2. For other classes in the same package, the inner class can be hidden, in other words, the inner class, regardless of the visibility of the method, is even public, except for the containment class, which cannot be used by other classes.
3. Anonymous inner classes can be easily defined as callbacks.
4. The use of internal classes makes it easy to write event drivers.

In fact, its real purpose is simply to define the callback--Further, the event-driven.
Interfaces and callbacks: programming a common pattern is the callback pattern, in which you can specify a method to callback the object when a particular time occurs.

Precautions:

This is in the anonymous class and in the inner class:
Sometimes we use some internal classes and anonymous classes. When this is used in an anonymous class, this refers to the anonymous class or the inner class itself.
Then if we want to use the methods and variables of the outer class, we should add the class name of the outer class. As the following example:

public class A {
  int i = 1;
  Public A () {
    thread thread = new Thread () {public
      void run () {for
        (;;) {
          a.this.run ();
          try {Sleep
            (1000);
          } catch (Interruptedexception IE) {
          }}}
    ;
    Thread.Start ();
  } 
  public void Run () {
    System.out.println ("i =" + i);
    i++;
  }
  public static void Main (string[] args) throws Exception {
    new A ();
  }
}

In the example above, thread is an anonymous class object, in which its run function uses the Run function of the external class.
This is because the function has the same name, the direct call is not. There are two ways, one is to change the external run function to a name, but this method for a development to Midway application is not advisable
。 Then you can use the method in this example to use the class name of the outer class plus this to describe the way run of the external class to invoke.

For this example:

this.test (New Inner () {public
             void Method1 () {
                 System.out.print ("1111");
           }
           
             public void Method2 () {
                 System.out.print ("22222");
           }
);

At this point, the test () method is called, and when is the method1 and method2 of the inner class called? Is this the same object sending them messages (such as passing in a parameter)? Or is it a direct explicit call??

For the inner class, in addition to this class, it is the This.test (...). This in the sentence, it can call the inner class method, nowhere else, however, it also requires you to have a place in the class to save a reference to the internal class instance. Again, the inner class is used to invoke the outside method at some point--this is the callback.
To illustrate the method of an internal class instance can only be invoked in an instance of an enclosing class, other places cannot be invoked, giving an example of the following:
JAVA2 Practical Tutorial Source code:

/** an application to demonstrate the use of internal classes.
/** class Outer * *
Class outer{
private static int size;
/** internal class Inner declaration * *
public class inner{
private int size;
/** method Dostuff () * *
public void Dostuff (int size) {
size++; Accessing local variables
this.size++; Accessing the member variables of its inner class
outer.this.size++; Accessing the member variables of its external class
System.out.println (size+ "" +this.size+ "" +outer.this.size);
}
}//Inner class inner end
The instance method defined in the/** class outer Testinner () method * *
public void Testinner () {
Inner i=new Inner ();
I.dostuff (5);
}
/** Main () method * *
public static void Main (string[] a) {
Outer o=new Outer ();
O.testinner ();
}
}//class outer End

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.