• Post Reply Bookmark Topic Watch Topic
  • New Topic

Packages  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys first of all I fixed the error but I really want to know why I am even getting an error in this code because there is no naming conflicts I even used the package name before Color when setting the color on my label but I still got an error which was pointing me to the import javafx.scene.Paint.Color line but when I deleted import com.sun.prism.paint.Color; it compiled fine

how come there was a confliction when both Color classes are from different packages and to be clear that they are from different packages I specified that with the package name before Color?

thanks

 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us the error message before you deleted the first Color import. If you are using an IDE hover your mouse over the red marks and note what the popup says.

Beware of importing packages whose name starts com.sun. They may be removed in future releases. Beware of Eclipse's autocomplete feature for imports. You write Col‑ctrl‑space and you get a dropdown list of imports in Col…. It is very easy to click the first Color which appears, and get the wrong import.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Campbell

here is the error

Error:(13, 1) java: a type with the same simple name is already defined by the single-type-import of com.sun.prism.paint.Color

I'm using itelij for JavaFx mainly and Eclipse for console programs

thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam,
Java doesn't allow this:


The idea is that with an import, Java knows what package to use when you type "Color" later in the code. But here, you said to use both. So what does Color mean?

It's ambiguous. The solution is to remove one (or both) imports and instead type out the fully qualified name when referring to it in the class. For example, type "com.sun.prism.paint.Color" instead of "Color"
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Jeanee that makes sense but just out of curiosity why wouldn't Java allow you to use both?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:thanks Jeanee that makes sense but just out of curiosity why wouldn't Java allow you to use both?

You can use both. You just have to use the fully qualified name for one (or both.)

Pretend you are in elementary school and there is another kid named Adam in your class. The teacher says "Adam, please come to the front of the room". How do you know which of the boys she wants? This is the import problem. Java doesn't know which Color you want. Whereas if the teacher says "Adam Chalkley, please come to the front of the room", you know who she wants. This is the fully qualified import.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good analogy thanks Jeanne =)
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you probably don't want the sun.com import at all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!