编译失败:

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. a + b ——C语言初学者百题大战之四

    #include <stdio.h> int main() { int a,b; scanf("%d %d",&a,&b); printf(" ...

  2. Scala高手实战****第18课:Scala偏函数、异常、Lazy值编码实战及Spark源码鉴赏

    本篇文章主要讲述Scala函数式编程之偏函数,异常,及Lazy 第一部分:偏函数 偏函数:当函数有多个参数,而在使用该函数时不想提供所有参数(比如函数有3个参数),只提供0~2个参数,此时得到的函数便 ...

  3. [转] Matlab与C++混合编程(依赖OpenCV)

    作者 zouxy09@qq.com,原文 Matlab与C++混合编程(依赖OpenCV) 之前在运行别人论文的代码的时候,经常有遇到Matlab与C++混合编程的影子.实际上就是通过Matlab的M ...

  4. SVN安装中遇到的问题

    新的版本:1.9.5 必须使用Apache Portable Runtime Utility 1.5.4 Released没有安装的话需要先安装 需要安装apr.apr-util sqlite zli ...

  5. openstack 动态加载usb,需要修改kvm虚拟机的xml文件

    一.利用libvirt命令动态挂载 在利用KVM的虚拟桌面应用中,有时候需要在虚拟桌面起来后还能够动态的挂载或卸载数据盘,以达到类似热插盘U盘或移动硬盘的效果,当然管理上需要做处理.如果纯粹中技术上来 ...

  6. Listener监听器之HttpSessionListener

    编写一个OnlineUserListener. package anni; import java.util.List; import javax.servlet.ServletContext; im ...

  7. PHP性能优化大全

    第一章  针对系统调用过多的优化 我这次的优化针对syscall调用过多的问题,所以使用strace跟踪apache进行分析. 1.  apache2ctl -X & 使用-X(debug)参 ...

  8. Laravel 5系列教程四:数据库和Eloquent

    免费视频教程地址https://laravist.com/series/laravel-5-basic 上一篇写了一些Laravel Blade的基本用法和给视图传递变量的几种方式, 这一节我们来说说 ...

  9. Laravel 5系列教程二:路由,视图,控制器工作流程

    免费视频教程地址https://laravist.com/series/laravel-5-basic 上一篇教程我们走了那么长的路,终于把Laravel安装好了,这一篇教程我们就要进入Laravel ...

  10. window7下面安装pear.pchar--wamp环境

    准备工作: Wamp php版本:5.3.10 1.下载pear.phar 2.设置php路径的path环境变量 开始安装 1.以管理员身份运行cmd 2.进入pear.phar的所在目录 3.命令行 ...