8

First of all, I am a new to java, so my question might be stupid but i still need an answer :)

I have a class that handle display matters. I have named it "Display", but the problem is : I need to import a class called org.lwjgl.opengl.Display.

Of course, I have this error at my Display class statement :

"Display" is already defined in this compilation unit

And of course, I can rename my class, but i'd like to be sure there is no way to easily circumvent this issue.

In a general way (because using a game library such as LWJGL, I guess i will have plenty of this), is it a better idea to prefix all my class to avoid similar label ?

Update : The class is already in a package.

package Graphics;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class Display { ... }

Thanx.

Cornelius
  • 378
  • 1
  • 2
  • 12

2 Answers2

33

If you can't rename your own class, which would be the easiest, then you can circumvent this by not importing the offending class and instead using the fully qualified package name, e.g

org.lwjgl.opengl.Display display = new org.lwjgl.opengl.Display().

Conversely, you should put your own class in packages and never use the default package, so that it's possible to apply the same method to disambiguate your own classes.

JRL
  • 76,767
  • 18
  • 98
  • 146
  • 1
    @Cornelius: don't import the Display class, use the fully qualified name when you need it, and it will compile. – JRL Sep 25 '11 at 17:53
  • Ok, i thought it was not a proper way to do this. – Cornelius Sep 25 '11 at 17:56
  • 20
    Does any version of Java have a feature to alias fully qualified names into short names so you don't really have to put the fully qualified name everywhere? Something like `import org.lwjgl.opengl.Display as OglDisplay;` then `OglDisplay display = new OglDisplay();` ? – ADTC Jun 06 '13 at 09:02
0

This happens when your java class name and importing library name is same. In your case Scanner is refer to the class name not for the library. Changing the class name to something else will be the simplest way to solve the error.

public class Foo {

private static class Display {...}

}

will also help to solve the issue.

Thilina Koggalage
  • 1,044
  • 8
  • 16