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:

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

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.

  1. Main.java:17: error: cannot reference this before supertype constructor has been called
  2. super(getClass().getClassLoader()); // should not call getClass() because object
  3. ^
  4. 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. 即时编译和打包您的 Groovy 脚本(转)

    在本文中将会涉及到: 使用 CliBuilder 来实现对命令行选项的支持,脚本执行时所需要的参数将通过命令行选项的方式传递. 使用 GroovyClassLoader 加载 Groovy class ...

  2. Java贪吃蛇游戏

    package snake.game; import java.awt.CardLayout; import java.awt.Color; import java.awt.Graphics;   i ...

  3. c# ThreadPoold使用心得

    于c#多线程编程经常使用的线程,但是,因为线程的创建和销毁是非常资源 - 成本非常大.因此,我们使用线程池来解决问题, 在线程池的开始是申请一定数量的线程系统.和维护,有任务时间,假设你有空闲的线程, ...

  4. Sliverlight之 矢量绘图

    目标:在两天内完成一个环形图的绘制 准备:第5章 矢量绘图 1,形状绘图(见Project11) (1)线条用什么标签表示,它有哪几个重要属性 说明: Line标签 x1 y1表示起始点x,y坐标 x ...

  5. 重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口

    原文:重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口 [源码下载] 重新想象 Windows 8 Store Apps (2 ...

  6. 【原创】leetCodeOj --- Copy List with Random Pointer 解题报告

    题目地址: https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题目内容: A linked list is given s ...

  7. 使用BackgroundWorker组件进行异步操作编程

    本文介绍了BackgroundWorker组件的功能及在基于事件的异步操作编程中的应用,并对组件的实现原理进行简述.在应用程序中,可能会遇到一些执行耗时的功能操作,比如数据下载.复杂计算及数据库事务等 ...

  8. ps命令用法详解(转)

    ps p 22763  -L -o pcpu,pid,tid,time,tname,cmd,pmem,rss --sort rss  按rss排序 ps p 26653 -L -o pcpu,tid ...

  9. java编程接口(6) ------ 图标

    本文提出了自己的学习笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 能够在JLable或者不论什么从AbstractButton继承的组件使用Ic ...

  10. 一篇非常全面的freemarker教程

    copy自http://demojava.iteye.com/blog/800204 下面内容所有是网上收集: FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主 ...