1

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?

Ryan Dougherty
  • 528
  • 6
  • 21
  • 1
    works with `private static void Integer()` as well. – Luiggi Mendoza Jun 26 '14 at 21:07
  • Or even `Test()` - this is a common mistake. Newbies often add a return type when trying to create a constructor thereby creating a strangely named method. – Boris the Spider Jun 26 '14 at 21:10
  • possible duplicate of [What is the need of Void class in Java](http://stackoverflow.com/questions/2352447/what-is-the-need-of-void-class-in-java) –  Jun 26 '14 at 21:10
  • 1
    @naimdjon Replace the `java.lang.Void` with `java.lang.Integer`. Does that linked question still answer this one? – Sotirios Delimanolis Jun 26 '14 at 21:11
  • 1
    You can even create your own class or method named `String`, or `Object`. It's not smart because you're going to make a very confusing program, but these are just names, not any more special than other names in a Java program. – Jesper Jun 26 '14 at 21:19
  • 2
    There are **numerous** classes in java. Forbidding any method to have the same name as any class would be untenable – Richard Tingle Jun 26 '14 at 21:20
  • 1
    @naimdjon `Void` is just an example, this isn't about what `Void` is for – Richard Tingle Jun 26 '14 at 21:28

2 Answers2

10

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.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 7
    Note that you're free to do this but then s̶u̶f̶f̶e̶r̶ ̶f̶o̶r̶ ̶y̶o̶u̶r̶ ̶b̶a̶d̶ ̶d̶e̶s̶i̶g̶n̶ ̶c̶h̶o̶i̶c̶e̶s̶ deal with the side effects of this design decision. – Luiggi Mendoza Jun 26 '14 at 21:09
0

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.

mittelmania
  • 3,393
  • 4
  • 23
  • 48
  • Re your first paragraph. It's worth noting that there's no primative `void` – Richard Tingle Jun 26 '14 at 21:41
  • I never stated that void is a primitive type – mittelmania Jun 30 '14 at 14:33
  • In that case what is your first paragraph trying to say? – Richard Tingle Jun 30 '14 at 14:34
  • I'm merely explaining that everything in Java is an object, including primitive types. Otherwise, it would be difficult to understand why there's a Void class. – mittelmania Jun 30 '14 at 14:40
  • I wouldn't say thats strictly speaking true. You could remove the Double, Float etc classes from java and retain the primatives. The primatives are explicitly not objects. The boxed versions are just provided for convenience for use with ArrayLists etc. I know in more recent versions autoboxing and autounboxing has made them more are more closely related to their primative cousins but in early versions of java they lived largely seperate lives (much like a `Vector2d` contains 2 doubles a `Double` contains 1 `double`) – Richard Tingle Jun 30 '14 at 14:58
  • Btw `java.lang.*` is automatically imported, and it doesn't matter if class `Foo` is imported you can still have a method called `Foo` as well. For example the following is fine; `public String String(){return "hello";}` – Richard Tingle Jun 30 '14 at 15:04