编译失败:

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. }

转-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. 【分类讨论】Codeforces Round #395 (Div. 2) D. Timofey and rectangles

    D题: 题目思路:给你n个不想交的矩形并别边长为奇数(很有用)问你可以可以只用四种颜色给n个矩形染色使得相接触的 矩形的颜色不相同,我们首先考虑可不可能,我们分析下最多有几个矩形互相接触,两个时可以都 ...

  2. 【暴力】UVALive - 4882 - Parenthesis

    就不断地扫整个序列,如果发现多余的括号就删除.大概复杂度还是O(n²)左右.如何判断不合法请详见代码. To a computer, there is no difference between th ...

  3. 【Trie】bzoj1212 [HNOI2004]L语言

    枚举每个文章里已经在Trie中被标记为可能是分割处的字符,然后再从此处跑Trie,继续向后标记.由于单词数很少,因此复杂度可以接受,O(n*m*Len). #include<cstdio> ...

  4. Vimperator常用快捷键

    分别往下/往上滚动窗口一行 j/k 左右滚动窗口     h/l 向下/向上滚动一屏的窗口 <Space>/<C-b> 向下/向上滚动半屏的窗口 <C-d>/< ...

  5. centos系统的时间时区和MySQL的时间时区问题

    原文:http://1567045.blog.51cto.com/1557045/1074971 centos系统的时间时区和MySQL的时间时区问题  年轻人做事要细心,特别我们这些搞IT的千万不莽 ...

  6. oracle 百万行数据优化查询

      1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使 ...

  7. 织梦默认分页样式改动 解决分页列表显示,去掉li

    近期装了个织梦dedecmsV5.7版本号时,调用分页显示出现的结果出现好几行,怎么也不能在一排显示,找了非常多资料,才了解到是由织梦模板中分页加了<Li>列表标签,解决有两种方法,以下将 ...

  8. MongoDB分片集群新增分片(自用)

    机器IP为192.168.58.11,计划在上面新建两个分片并添加到原有分片集群中. 实施如下: 1.58.11创建mongodb文件夹 mkdir -p /opt/mongodb cd  /opt/ ...

  9. orchard project 本地化

    http://orchardproject.net/localization 本地化 果园的本地化管理是托管在一个外部服务( Crowdin), 的项目.公众和贡献是受欢迎的! 如何做出贡献 注册上 ...

  10. 在redhat下使用x11vnc进行桌面共享

    1.在redhat上安装x11vnc时.你须要注意下面几个方面: (1)下载x11vnc的源代码包: 网址例如以下所看到的: http://sourceforge.net/projects/libvn ...