在拥有外部类对象之前是不可能创建内部类对象的,因为内部类对象会暗暗地连接到创建它的外部类对象上;但是嵌套类(静态内部类)除外。

    1. public class Parcel3 {
    2. class Contents{
    3. private int i;
    4. public int value(){return i;}
    5. }
    6. //静态内部类
    7. static class Destination{
    8. String label;
    9. Destination(String to){
    10. label = to;
    11. }
    12. String getLabel(){
    13. return label;
    14. }
    15. }
    16. public static void main(String[] args) {
    17. final Parcel3 parcel3 = new Parcel3();
    18. final Contents contents = parcel3.new Contents();
    19. final Destination aa = new Destination("aa");
    20. }
    21. }

    关于内部类访问权限的问题

    public interface Contents{
      int value();
    }
    public interface Destination{
      String readLabel();
    }
    
    
    //验证在其他包下面能否访问到PContents\PDestination 及其方法
    public class TestParcel {
    
      //先看类得访问权限 再看域或方法得访问权限
      private class PContents implements Contents{
        private int value = 11;
        @Override
        public int value() {
          return value;
        }
      }
      protected class PDestination implements Destination{
        private String s;
        PDestination(String whereTo){
          s = whereTo;
        }
        @Override
        public String readLabel() {
          return s;
        }
      }
    
      public static void main(String[] args) {
        final TestParcel parcel = new TestParcel();
        PContents pContents = parcel.new PContents();
        pContents.value();
    
        final PDestination destination = parcel.new PDestination("Twiwan");
        System.out.println(destination.readLabel());
      }
    }