编译失败:

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. UVA 10160 Servicing Stations(状态压缩+迭代加深)

    [题目链接] LInk [题目大意] 给出一些点和边,选择一个点就能把这个点和相邻的点都覆盖,求最小点覆盖 [题解] 我们压缩点被覆盖的状态,迭代加深搜索覆盖的最小点数, 当剩余的点全部选上时都无法完 ...

  2. 【矩阵哈希】【二分答案】【哈希表】bzoj1567 [JSOI2008]Blue Mary的战役地图

    引用题解:http://hzwer.com/5153.html 当然,二分可以换成哈希表. #include<cstdio> #include<iostream> #inclu ...

  3. 【Treap模板详细注释】BZOJ3224-普通平衡树

    模板题:D错因见注释 #include<iostream> #include<cstdio> #include<cstring> #include<algor ...

  4. [CF627D]Preorder Test

    题目大意: 一个$n(n\le2\times10^5)$个结点的树,每个结点有一个权值$w_i$.可以任选一点为根,并选择一些结点交换其子结点的顺序,使得该树DFS序上第$m$个结点的权值最大.求最大 ...

  5. [CF911B]Two Cakes

    题目大意: 有两种蛋糕,分别被切成了a块和b块,要把这些蛋糕分到n个盘子里. 要求每个盘子里只能有一种蛋糕,每一种蛋糕都被分.问最优情况下,盘子里至少能放几个蛋糕. 思路: 二分答案. 由于每个蛋糕都 ...

  6. js创建json对象

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. SQL Server Wait Types Library

    https://www.sqlskills.com/blogs/paul/announcing-the-comprehensive-sql-server-wait-types-and-latch-cl ...

  8. 【报错】引入jar包import org.apache.commons.codec.digest.DigestUtils 报错,jar不存在

    import org.apache.commons.codec.digest.DigestUtils报错.缺少jar maven引用jar包地址: <!-- https://mvnreposit ...

  9. 【java】字符串的反转

    @org.junit.Test public void test(){ String a = "I IOVE CHINA"; if(a.indexOf(" ") ...

  10. asp.net 视图引擎归类

    1. ASPX View Engine 第一个也是我们最熟悉的---aspx,相信做过WebForm开发对Aspx都比较了解: 小示例: <%@ Control Inherits="S ...