Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is every class an instance of Class

Tags:

java

The code below won't compile, why?

System.out.println(Void.class instanceof Class);

This won't compile either

//according to oracle doc, the type of Void.TYPE is Class<Void>
System.out.println(Void.TYPE instanceof Class);

The error message is

VoidTest.java:6: inconvertible types found : java.lang.Class<java.lang.Void> required: Class System.out.println(Void.TYPE instanceof Class); 

And i am using Darwin Kernel Version 12.4.0 and Java version "1.6.0_51"

They won't compile using emacs + java + javac

They compile fine using online repl:http://www.javarepl.com/console.html or use eclipse on my machine

like image 965
charles_ma Avatar asked Jan 22 '26 16:01

charles_ma


1 Answers

Promoting a comment to an answer...

I was able to duplicate the compiler error until I noticed I had a Class.class file in my directory (probably from attempting to analyze a past StackOverflow question).

I deleted that file, and then it compiled and ran just fine.

$ javac Main.java
Main.java:7: inconvertible types
found   : java.lang.Class<java.lang.Void>
required: Class
        System.out.println(Void.class instanceof Class);
                               ^
Main.java:8: inconvertible types
found   : java.lang.Class<java.lang.Void>
required: Class
        System.out.println(Void.TYPE instanceof Class);
                               ^
2 errors
$ rm Class.class
$ javac Main.java
$ java Main
true
true

Some custom Class class had taken precedence over the built-in java.lang.Class.

like image 186
rgettman Avatar answered Jan 24 '26 08:01

rgettman