一个很神奇的问题 为什么会报 Java .lang.InstantiationError 错误呢?

2019-08-06 20:36:33 +08:00
 lizhiping886
public class findDupicate {

    public static void main(String[] args) {

        animal i1 = new animal();
        cat i2 = new cat();
        

    }

}

class cat {
    public int age = 1;

    public cat() {
        System.out.println("this cat init ");
    }

    public void bark(){
        System.out.println("this is animal");
    }
    
}

class animal {
    public int legNum = 1;

    public animal(){
        System.out.println("this animal init ");
    }

    public void bark(){
        System.out.println("this is animal");
    }
}

输出是这样的

this animal init 
Exception in thread "main" java.lang.InstantiationError: cat
	at findDupicate.main(findDupicate.java:6)
    

为什么 animal 可以实例化成功 而 cat 不行呢?

jdk 版本是 open-jdk 12

4221 次点击
所在节点    Java
6 条回复
chendy
2019-08-06 21:03:25 +08:00
没有报错…不知所措…
leonme
2019-08-06 21:50:34 +08:00
没有报错,另外注意类名命名规范
ninjachen
2019-08-07 01:06:03 +08:00
感觉不会报错。
类名首字母大写
oneisall8955
2019-08-07 08:05:08 +08:00
不会错吧,看着没毛病。另外,注意命名规范
难不成是 jdk 的锅?
lizhiping886
2019-08-07 08:21:43 +08:00
我换个 jdk 试试
LeeSeoung
2019-08-07 09:40:22 +08:00
在 JDK 8\11\12 都正常运行。还有注意类名首字母大写。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/589620

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX