用 "this.new InnerClass()" 和 "new InnerClass()" 实例化的区别
Difference between instantiating with "this.new InnerClass()" and "new InnerClass()"
我正在审查一些 material 的 java 8 认证,并遇到了类似于下面所示的代码。有人可以解释使用 'this' 关键字实例化内部 class 和不使用 'this' 关键字之间的区别吗?两种实例化 InnerClass 的方式似乎都有效(没有编译器错误或运行时错误)。
这是否类似于使用 class 名称访问 public 静态成员而不是实例名称?两种方法都有效,但使用 class 名称优于使用实例名称。
public class OuterClass {
class InnerClass { } // Define Inner class
private InnerClass innerclass; // private member
private InnerClass thisInnerClass; // private member
public OuterClass() {
this.innerclass = new InnerClass(); // without 'this' keyword
this.thisInnerClass = this.new InnerClass(); // with 'this' keyword
}
}
=====================================
编辑:
两者没有区别。
我编译了以下两个版本,两个版本的 md5 和是相同的。使用 'this' 引用和不使用 'this' 引用生成相同的字节码。
public class OuterClass {
class InnerClass { } // Define Inner class
private InnerClass innerClass; // private member
public OuterClass() {
this.innerClass = new InnerClass(); // without 'this' keyword
}
}
MD5 Sum for compiled above code (without 'this' reference):
MD5 (OuterClass$InnerClass.class) = 7f1679f1c7a0201164ce5eb03fe29699
MD5 (OuterClass.class) = bf7419b01f8f7c24d2892d10c4fd6e05
public class OuterClass {
class InnerClass { } // Define Inner class
private InnerClass innerClass; // private member
public OuterClass() {
this.innerClass = this.new InnerClass();// with 'this' keyword
}
}
MD5 Sum for compiled above code (with 'this' reference):
MD5 (OuterClass$InnerClass.class) = 7f1679f1c7a0201164ce5eb03fe29699
MD5 (OuterClass.class) = bf7419b01f8f7c24d2892d10c4fd6e05
我的理解是两者没有区别。这就像从 class 中调用 foo()
或 this.foo()
foo()
包含在中。 this
在未明确写入时是隐含的。
我正在审查一些 material 的 java 8 认证,并遇到了类似于下面所示的代码。有人可以解释使用 'this' 关键字实例化内部 class 和不使用 'this' 关键字之间的区别吗?两种实例化 InnerClass 的方式似乎都有效(没有编译器错误或运行时错误)。
这是否类似于使用 class 名称访问 public 静态成员而不是实例名称?两种方法都有效,但使用 class 名称优于使用实例名称。
public class OuterClass {
class InnerClass { } // Define Inner class
private InnerClass innerclass; // private member
private InnerClass thisInnerClass; // private member
public OuterClass() {
this.innerclass = new InnerClass(); // without 'this' keyword
this.thisInnerClass = this.new InnerClass(); // with 'this' keyword
}
}
=====================================
编辑: 两者没有区别。 我编译了以下两个版本,两个版本的 md5 和是相同的。使用 'this' 引用和不使用 'this' 引用生成相同的字节码。
public class OuterClass {
class InnerClass { } // Define Inner class
private InnerClass innerClass; // private member
public OuterClass() {
this.innerClass = new InnerClass(); // without 'this' keyword
}
}
MD5 Sum for compiled above code (without 'this' reference):
MD5 (OuterClass$InnerClass.class) = 7f1679f1c7a0201164ce5eb03fe29699
MD5 (OuterClass.class) = bf7419b01f8f7c24d2892d10c4fd6e05
public class OuterClass {
class InnerClass { } // Define Inner class
private InnerClass innerClass; // private member
public OuterClass() {
this.innerClass = this.new InnerClass();// with 'this' keyword
}
}
MD5 Sum for compiled above code (with 'this' reference):
MD5 (OuterClass$InnerClass.class) = 7f1679f1c7a0201164ce5eb03fe29699
MD5 (OuterClass.class) = bf7419b01f8f7c24d2892d10c4fd6e05
我的理解是两者没有区别。这就像从 class 中调用 foo()
或 this.foo()
foo()
包含在中。 this
在未明确写入时是隐含的。