Static inner class of Java

Source: Internet
Author: User
Tags abstract static class

To correctly understand the meaning of static when applied to an inner class, it is important to remember that the object of the inner class defaults to the handle of an object of the encapsulated class that created it. However, if we say that an inner class is static, this argument is not tenable. The static inner class means:
(1) To create an object of a static inner class, we do not need an external class object. The
(2) cannot access an external class object from an object in the static inner class.
But there are limitations: because a static member can only be at the external level of a class, an inner class cannot have static data or a static inner class.
If you want to create an object for an inner class without creating an object of the outer class, you can set everything to static. In order to work properly, you must also set the inner class to static. As follows:
 

//: Parcel10.java//Static inner classes package c07.parcel10;

Abstract class Contents {abstract public int value ();}

Interface Destination {String readlabel ();}
    public class Parcel10 {private static class Pcontents extends Contents {private int i = 11;
  public int value () {return i;}
    Protected static class Pdestination implements destination {private String label;
    Private Pdestination (String whereto) {label = Whereto;
  Public String Readlabel () {return label;}
  public static destination Dest (String s) {return new pdestination (s);
  public static Contents cont () {return new pcontents ();
    public static void Main (string[] args) {Contents c = cont ();
  Destination D = dest ("Tanzania"); }
} ///:~


In main (), we don't need Parcel10 objects; Instead, we use regular syntax to select a static member to invoke the method that returns the handle to contents and destination.
Normally, we don't set any code in an interface, but a static inner class can be part of an interface. Because the class is "static", it does not violate the rules of the interface--static internal classes are located only within the namespace of the interface:

: Iinterface.java
//Static inner classes inside interfaces interface IInterface

{
  Static class Inner {
   
    int I, J, K;
    Public Inner () {}
    void F () {}
  }
}///:~
   

Earlier in the book, I suggested that you set a main () in each class to use as a test bed for that class. One drawback to this is that there are too many extra code. If you don't want to, consider using a static inner class to accommodate your own test code. As shown below:

: Testbed.java
//Putting test code in a static inner class

class Testbed {
  testbed () {}
  void F () { System.out.println ("F ()"); Public
  Static class Tester {public
    static void Main (string[] args) {
      testbed t = new testbed ();
      T.f ();}}
///:~

This generates a separate class named Testbed$tester (to run the program, use the Java testbed$tester command). You can use this class for testing, but you do not need to include it in your final release version.

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.