转-Cannot refer to an instance field arg while explicitly invoking a constructor

编译失败:

Cannot refer to an instance field arg while explicitly invoking a constructor  调用方法是不能引用一个实例变量

 1 package arkblue.lang.javapuzzler.n53;
 2
 3 class Thing {
 4     public Thing(int i) {
 5
 6     }
 7 }
 8
 9 public class MyThing extends Thing {
10     private final int arg;
11
12     public MyThing() {
13         super(arg = Math.round(12L)); //编译失败
14     }
15
16 }

解决办法:使用了交替构造器调用机制(alternate constructor invocation)

在这个私有构造器中,表达式SomeOtherClass.func()的值已经被捕获到了变量i中,并且它可以在超类构造器返回之后存储到final类型的域arg中

 1 class SomeOtherClass {
 2     static int func() {
 3         return Math.round(12L);
 4     }
 5 }
 6
 7 public class MyThing extends Thing {
 8     private final int arg;
 9
10     public MyThing() {
11         this(SomeOtherClass.func());
12     }
13
14     private MyThing(int i) {
15         super(i);
16         arg = i;
17     }
18 }
时间: 2024-11-05 18:50:37

转-Cannot refer to an instance field arg while explicitly invoking a constructor的相关文章

object is not an instance of declaring class while invoking public abstract…的解决

如题所示,使用Apache CXF开发web service时,使用SoapUI对web service接口进行测试时报了以下错误: org.apache.cxf.interceptor.Fault: object is not an instance of declaring class while invoking public abstract java.lang.String cn.zifangsky.service.CXFService.sayHello(java.lang.Strin

Akka 2 Actor 源码

Actor源码研究,先附上源码 // ...... object Actor {   /**    * Type alias representing a Receive-expression for Akka Actors.    */   //#receive   type Receive = PartialFunction[Any, Unit]   //#receive   /**    * emptyBehavior is a Receive-expression that matche

Akka源码分析-Actor&ActorContext&ActorRef&ActorCell

分析源码的过程中我们发现,Akka出现了Actor.ActorRef.ActorCell.ActorContext等几个相似的概念,它们之间究竟有什么区别和联系呢? /** * Actor base trait that should be extended by or mixed to create an Actor with the semantics of the 'Actor Model': * <a href="http://en.wikipedia.org/wiki/Actor

【反射】Class Field Method Constructor

Class public final class java.lang.Class<T> extends Object implements Serializable, GenericDeclaration, Type, AnnotatedElement 类型参数 T:由此 Class 对象建模的类的类型.例如, String.class 的类型是 Class<String>.如果将被建模的类未知,则使用 Class<?>. Instances of the class

A const field of a reference type other than string can only be initialized with null Error [duplicate]

I'm trying to create a 2D array to store some values that don't change like this. const int[,] hiveIndices = new int[,] { {200,362},{250,370},{213,410} , {400,330} , {380,282} , {437, 295} , {325, 405} , {379,413} ,{343,453} , {450,382},{510,395},{46

[ThreadStatic] dosen&#39;t work with instance fields

ThreadStatic 属性对于字段无效 最近使用RESHARPER,发现有这个提示,查了下资料 Occa­sion­ally you might want to make the value of a sta­tic or instance field local to a thread (i.e. each thread holds an inde­pen­dent copy of the field), what you need in this case, is a thread-lo

webservice调用报object is not an instance of declaring class错

错误原文: 警告: Application {http://service.core.ws.component.creditease.com/}NLender2CoreWebServiceService#{http://service.core.ws.component.creditease.com/}riskFundInfoNotice has thrown exception, unwinding noworg.apache.cxf.interceptor.Fault: object is 

Spring约束

时间:2017-1-29 02:01 Appendix D. XML Schema-based configuration Prev Part VII. Appendices Next Appendix D. XML Schema-based configuration D.1 Introduction This appendix details the XML Schema-based configuration introduced in Spring 2.0 and enhanced an

Android调用JNI本地方法经过有点改变

方法注册好后要经过哪些路 Android一个异常捕获项目 https://github.com/xroche/coffeecatch coffeecatch CoffeeCatch, a tiny native POSIX signal catcher (especially useful for JNI code on Android/Dalvik, but it can be used in non-Java projects) It allows to "gracefully"