简体   繁体   中英

Should java allow classes names to be defined same as generic type parameters?

I created classes like below in the same package test . It was just to test whether java really allows it. And it does but then it comes in to problems.

package test;

public class E {
}


package test;

public class T {
}


package test;

import test.E;
import test.T;

public class K<E, T> {

E e;
T t;

public K(E e, T t) {
    this.e = e;
    this.t = t;
}

public static void main(String[] args) {
    K k = new K<E, T>(new E(), new T());
}
}

Above code give multiple compilation problems

Multiple markers at this line
- Cannot make a static reference to the non-static type E
- Cannot make a static reference to the non-static type T
- Cannot make a static reference to the non-static type T
- Cannot make a static reference to the non-static type E
- K is a raw type. References to generic type K<E,T> should be 
 parameterized

It clearly shows compiler is confused between E and class E same for T.

So workaround is define it real types using package.

 K k = new K<test.E, test.T>(new test.E(), new test.T());

Now if there all these classes are in default package there is no way to solve this compilation issue. So Question is should java allow declaration of such classes in default package ?

It clearly shows compiler is confused between E and class E same for T.

I think you've got that wrong. I think that if you read the relevant parts of the JLS carefully (I'll look them up later) you will find that they clearly state what E and T should resolve to in the various contexts. I would be very surprised if the compiler is getting the resolution wrong; ie not implementing the JLS.

In reality, the confusion is in the mind of the person who wrote the code ...

The problem here is that the rules about what takes precedence over what are probably not what you (and typical programmers) expect. But they are like they are for a good reason.

Normally this doesn't matter, but if you ignore the normal Java naming conventions and use one-letter names for classes, then you might get burnt.

So Question is should java allow declaration of such classes in default package?

Alternatively, should "you" be ignoring the Java class naming conventions?

Frankly, there are a lot of ways that a programmer can hurt themselves if they ignore the style guidelines / recommendations. But if you try to protect the programmer too much, you actually hurt him/her by making it impossible to implement stuff where you need to push the envelope. The best policy is (IMO) to not treat programmers as children. If they really want to juggle with sharp knives ... let them.

This is very nice research.

But, you are still allowed to create a class with name String , Java never complained against using same class name. To differentiate whether you use your String class (or) Java provided String class, you need to append the package (full name).

As Matt Ball said, default packages are shouldn't be used.

Backward compatibility could be another reason why Generic Types not defined as "reserve words"

Whether allow same class name (or) not, I think that is what packages are for. As long as there is a way to differentiate which class we are referring to, I think it is perfectly fine to allow same name.

You can get really confused if you want to. I am not sure its up to the compiler to prevent you from writing confusing code but I do think the compiler should try to be clear in its error messages.

public class T<T> {
    public T() {
    }

    public static <T> T T() {
        T T = null;
        return T; // which T is this?
    }
}

Considering you can write

public class String<Object>{}

Disalowing class with same name as how YOU named type parameter or forbiding you to name type parameter as any existing class would be insane (Class with conflicting name can be from another jar created in future, so any name of type parameter can be same as name of some class, and vice versa)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM