编译失败:

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

 package arkblue.lang.javapuzzler.n53;

 class Thing {
public Thing(int i) { }
} public class MyThing extends Thing {
private final int arg; public MyThing() {
super(arg = Math.round(12L)); //编译失败
} }

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

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

 class SomeOtherClass {
static int func() {
return Math.round(12L);
}
} public class MyThing extends Thing {
private final int arg; public MyThing() {
this(SomeOtherClass.func());
} private MyThing(int i) {
super(i);
arg = i;
}
}

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

  1. Cannot refer to an instance field pageTitle while explicitly invoking a cons

    当下面这样时在第7行会提示:Cannot refer to an instance field pageTitle while explicitly invoking a cons public cl ...

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

    分析源码的过程中我们发现,Akka出现了Actor.ActorRef.ActorCell.ActorContext等几个相似的概念,它们之间究竟有什么区别和联系呢? /** * Actor base ...

  3. Effective Java 31 Use instance fields instead of ordinals

    Principle Never derive a value associated with an enum from its ordinal; store it in an instance fie ...

  4. Effective Java 77 For instance control, prefer enum types to readResolve

    The readResolve feature allows you to substitute another instance for the one created by readObject ...

  5. 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[,] hiveI ...

  6. android jni ——Field & Method --> Accessing Field

    现在我们知道了怎样使用native code访问简单的数据类型和引用参考类型(string,array),下面我们来介绍怎样让jni代码去访问java中的成员变量和成员函数,然后可以再jni中回调ja ...

  7. 【反射】Reflect Class Field Method Constructor

    关于反射 Reflection 面试题,什么是反射(反射的概念)? 主要是指程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义 ...

  8. [转载] google mock cookbook

    原文: https://code.google.com/p/googlemock/wiki/CookBook Creating Mock Classes Mocking Private or Prot ...

  9. 译:Spring框架参考文档之IoC容器(未完成)

    6. IoC容器 6.1 Spring IoC容器和bean介绍 这一章节介绍了Spring框架的控制反转(IoC)实现的原理.IoC也被称作依赖注入(DI).It is a process wher ...

随机推荐

  1. 【dfs序】【二分】【主席树】【分块】bzoj3351 [ioi2009]Regions

    http://dzy493941464.sinaapp.com/archives/96 那个SIZE貌似必须设成R*R/Q?不知为啥,自己算的不是这个的说. 本机AC,线上TLE. #include& ...

  2. 【动态规划】【最短路】【spfa】bzoj1207 [HNOI2004]打鼹鼠

    <法一>若打了一只鼹鼠后,还能打另一只,我们可以在它们之间连权值为1的边.于是答案就是 以m为终点的最长路长度+1.建反图,就是单源最长路. MLE TLE 一时爽. #include&l ...

  3. 【最近公共祖先】【块状树】CODEVS 1036 商务旅行

    在线块状树LCA模板. #include<cstdio> #include<vector> #include<algorithm> #include<cmat ...

  4. freedom isn't free

    财务自由(除去房和车) 第一阶段: 个人存款达到50万以上 第二阶段 个人存款100~200万 第三阶段 个人存款400万以上 第三阶段以上才能算实现了相对较好的财务自由!come on , boys ...

  5. Klaus Aschenbrenner--windbg

    http://www.sqlservercentral.com/blogs/aschenbrenner/?page=1

  6. HTML5:绘制图形

    canvas绘图通过属于 canvas 的 JavaScript 方法完成 针对不支持html5的IE浏览器 <!--[if IE]> <script type="text ...

  7. linux过滤ip地址

    一.系统版本 [root@zabbix-server tmp]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) 二.用awk ...

  8. Java开发中的23种设计模式详解 【转】

    创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 行为型模式,共十一种:策略模式.模板方法模式.观察者模式.迭代子模式.责任链模式.命令模式.备忘录模式.状态模式.访问 ...

  9. How to backup a remote PostgreSQL db and restore it locally?

    pg_dump and pg_restore 来备份和恢复数据库中的数据. 原文:  https://ksearch.wordpress.com/2012/09/28/how-to-backup-a- ...

  10. android 博客列表

    1.  Hongyang http://blog.csdn.net/lmj623565791/article/details/37970961