Reputation: 538
I have this code:
public class Test {
public static void main(String[] args) {
Void();
}
private static void Void() {
System.out.print("Hello");
}
}
Since java.lang.Void
is a class in Java, why can that be the method name?
Upvotes: 1
Views: 288
Reputation: 3561
Java is a fully object-oriented language. That means that everything, even primitive types (like int or double) has a class object in the JDK. Some examples include Integer
vs. int
, Boolean
vs. bool
and so forth. And, since every object in Java inherits from the class Object
, these enable you to use boxing and unboxing (explicitly casting a class instance as an Object instance) with every type, including primitive types.
Void
is simply a placeholder for the void
keyword in Java. It is an uninstantiable object which has no methods or properties. And since Void
's full name is java.lang.Void
, you can create a class object or even a method with the same name in your own namespace, as long as you haven't explicitly imported java.lang.Void
into your class file.
Upvotes: 0
Reputation: 124245
Void
is not reserved keyword (void
is but Java is case-sensitive so void
is not equal Void
). Similarly String
, Integer
, Boolean
and so on are also not reserved.
You can use Void
to name whatever you want: classes, methods, fields. But being able to do something doesn't automatically mean that you should. It is not wise creating confusion by naming methods (or something else) with names already used to represents something else.
Upvotes: 10