-1

Let's say I have this Java class:

package com.version1;
class Sample
{ ... }

And in another Sample class, I want to import the Sample class from com.version1 package:

package com.version2;  // Now it's a different package

import com.version1.Sample;  // From another package

public class Sample
{...}

Please note: I need to import a class with same name as the current class, not 2 classes with same name.

So the question: Importing two classes with same name. How to handle? is not similar.

Devvie
  • 13
  • 3
  • But what is the question here? Does that not work? The only problem I can see here is that you will have to always use it as `com.version1.Sample`, eg. when defining variables of that type. – Amongalen Jun 29 '20 at 12:38
  • 1
    Also I don't understand why you think that question you linked isn't a fitting duplicate: If you want to use multiple classes with the same name you have to use the full qualified name with all except one to do so. Your case is no different at all – OH GOD SPIDERS Jun 29 '20 at 12:40
  • The problem was an error in the import statement, but I solved it when I deleted the import and just used `com.version1.Sample` everywhere instead. – Devvie Jun 29 '20 at 12:42
  • I actually think the [question you linked to](https://stackoverflow.com/questions/2079823/importing-two-classes-with-same-name-how-to-handle) is a better duplicate candidate than [the currently linked on](https://stackoverflow.com/questions/2903166/meaning-of-the-import-statement-in-a-java-file) because the later is **way** to generic to be a duplicate of this. In total I think this is not strictly a duplicate (at least I can't find the exact duplicate). – Joachim Sauer Jun 29 '20 at 12:52

1 Answers1

1

You simply can't have two classes referenced by the same short name. So the solution is actually the same one as in the case of importing two classes with the same name:

You'll have to reference the other class by its fully qualified class name and your local one by its short name. You could even use the FQCN for both, if that helps readability in your case.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614