An article on classloading states that the method getClass() should not be called within a constructor because:

object initialization will be complete only at the exit of the constructor code.

The example they gave was:

public class MyClassLoader extends ClassLoader{
public MyClassLoader(){
super(getClass().getClassLoader()); // should not call getClass() because object
// initialization will be complete only at
// the exit of the constructor code.
}
}

However from what I know, the native final method getClass() will always return the java.lang.Class object of that object instance, regardless of where it's called (within the constructor or not).

Will calling getClass() within the constructor ever give us problems?

If so, what are some examples whereby calling getClass() within the constructor would give us errors?

asked Aug 29 '14 at 4:49
Pacerier
23.5k26120225
 

3 Answers

Will calling getClass() within the constructor ever give us problems? If so, what are some examples whereby calling getClass() within the constructor would give us errors?

Using getClass() in constructor in this manner will always result in a compilation error, since thiscannot be referenced before super() has been called.

Main.java:17: error: cannot reference this before supertype constructor has been called
super(getClass().getClassLoader()); // should not call getClass() because object
^
1 error

You can test it yourself on http://ideone.com/B0nYZ1.

The Class is ready, but the instance can't be used to reference the Class yet.

Still, you can use the Class reference in constructor, but you have to do it in a slightly different way: super(MyClassLoader.class.getClassLoader())

Also, you are free to use getClass() in your constructor after the supertype constructor has been called - as you already pointed out, the object is basically ready after that and the Class reference can be inferred from the instance.

http://stackoverflow.com/questions/25561873/is-it-always-safe-to-call-getclass-within-the-subclass-constructor

Is it always safe to call getClass() within the subclass constructor?(转)的更多相关文章

  1. java反射案例

     Java反射经典实例 2007-08-29 17:55:25 分类: Java Java提供了一套机制来动态执行方法和构造方法,以及数组操作等,这套机制就叫——反射.反射机制是如今很多流行框架的实现 ...

  2. Java:描述反射机制的作用?举几个反射的应用?

    比较全的解释了:JAVA反射机制 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的信息以及动态调用对象的方 ...

  3. Core Java Volume I — 5.1. Classes, Superclasses, and Subclasses

    5.1. Classes, Superclasses, and SubclassesLet's return to the Employee class that we discussed in th ...

  4. java反射温习一下

    public class LoveReflect { public static class Demo implements Serializable{ } public static void ma ...

  5. 使用APT减少MVP的冗余代码

    前言 不知道从何时起,移动端开发都开始采用MVP.我们在认识到MVP有点的时候,也不妨会察觉到它其实也有很多恼人的地方,比如,我们针对每种状态渲染不同的视图: private void renderI ...

  6. JAVA常用基础知识点[继承,抽象,接口,静态,枚举,反射,泛型,多线程...]

    类的继承 Java只支持单继承,不允许多重继承- 一个子类只能有一个父类- 一个父类可以派生出多个子类这里写图片描述子类继承了父类,就继承了父类的方法和属性.在子类中,可以使用父类中定义的方法和属性, ...

  7. 用私有构造器或者枚举类型强化Singleton属性

    1.Singleton指仅仅被实例化一次的类.Singleton通常被用来代表那些本质上唯一的系统组件,如窗口管理器或者文件系统.使类称为Singleton会使它的客户端调试变的十分困难,因为无法给S ...

  8. 3.JAVA语言基础部分—Class类与反射

    什么是Java反射机制? JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的以及动态调用对象的方法的功能称为 ...

  9. Java中的java.lang.Class API 详解

    且将新火试新茶,诗酒趁年华. 概述 Class是一个位于java.lang包下面的一个类,在Java中每个类实例都有对应的Class对象.类对象是由Java虚拟机(JVM)自动构造的. Class类的 ...

随机推荐

  1. 查看文章strncpy()功能更好的文章

    strncpy()功能 原型:extern char *strncpy(char *dest, char *src, int n);    使用方法:#include <string.h> ...

  2. 冒泡排序算法 C++和PHP达到

    冒泡排序是小元素向前或向后的大要素.两个相邻元件之间的比较结果更.交换也这两个元件之间发生.它是最慢的排序算法. 效率最低的算法. 时间复杂度: 它是最差时间复杂度为:O(n^2),冒泡排序最好的时间 ...

  3. ios在SQLite3基本操作

    iOS关于sqlite3操作 iPhone中支持通过sqlite3来訪问iPhone本地的数据库. 详细用法例如以下 1:加入开发包libsqlite3.0.dylib 首先是设置项目文件.在项目中加 ...

  4. SqlServer表EXCEL数据复制的另一种方法

    一个.SqlServer表中的数据复制到excel 1.新建查询,用sql语句把表数据读出来 2.然后,选择数据,右键.复制(也能够点击连同标题复制),拷贝到记事本中(不然会乱码) 3.然后再把记事本 ...

  5. centos安装和卸载软件

    ==如何卸载: 1.打开一个SHELL终端 2.因为Linux下的软件名都包括版本号,所以卸载前最好先确定这个软件的完整名称. 查找RPM包软件:rpm -qa ×××* 注意:×××指软件名称开头的 ...

  6. SQL 修改排序规则的问题 sql_latin1_general_cp1_ci_as

    在一个项目中遇到:用原来的数据库生成的脚本,然后部署到新的服务器上,数据库的SQL_Latin1_General_CP1_CI_AS 怎么查询出来汉字都是乱码了. 遂查解决方法. 需要执行这个 ALT ...

  7. ListView嵌套GridView显示不完整的解决方案

    转载注明出处:http://blog.csdn.net/allen315410/article/details/40152987 近期在做项目中,有个模块须要在ListView中嵌套一个GridVie ...

  8. * 类描写叙述:字符串工具类 类名称:String_U

    /****************************************** * 类描写叙述:字符串工具类 类名称:String_U * ************************** ...

  9. UVA 11992 - Fast Matrix Operations(段树)

    UVA 11992 - Fast Matrix Operations 题目链接 题意:给定一个矩阵,3种操作,在一个矩阵中加入值a,设置值a.查询和 思路:因为最多20列,所以全然能够当作20个线段树 ...

  10. CCProgressTo 和CCProgressTimer

    在cocos2d中相同提供了非常多表现图片和精灵的方式,上一篇其中提到的切换场景的方式之中的一个是顺或逆时针切入的方法,在图片上也能够使用,test里有一个样例介绍CCProgressTimer能够实 ...