This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes Naming conflicts: imported class and defined class have same name Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Naming conflicts: imported class and defined class have same name" Watch "Naming conflicts: imported class and defined class have same name" New topic
Author

Naming conflicts: imported class and defined class have same name

Joggari Reddy
Greenhorn

Joined: Dec 20, 2005
Posts: 5
subject: Can I import a class and declare a type with the same name?

How can I work around the nameing conflict given below.

There is a type called List in the package (java.awt.List)I am importing.

If I want to give my class exactly the same name(just for curiosity) how can I deal with it.

------------ this works OK ----------
import java.awt.*; // Lsit class is in this package

public class List { //some variables and methods }

// this works ok if I import the package.


But I prefer to just import one List class rather than the whole pacakge. Is there any way to deal with this???
---------------------
import java.awt.List ;

public class List { //some variables and methods }
// I am getting compilation error stating that List is already defiend in the compilation unit


TIA
Greg Charles
Bartender

Joined: Oct 01, 2001
Posts: 2550
    
  10

It is possible to define your class as List, and yet still use the java.awt.List inside it. However, you lose the ability to import java.awt.List in order to use List as a shorthand for it. Instead, you will have to use the fully-qualifed class name java.awt.List to refer to it.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
As a variation on this, if you are importing java.awt.* and java.util.* and want List to refer to java.util.List (not java.awt.List), you can do the following:
Does anyone still use java.awt.List?


There is no emoticon for what I am feeling!
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I used it in an example of why importing using * is a bad idea.

Other than that, no.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Naming conflicts: imported class and defined class have same name
 
Similar Threads
Problem with importing classes and finding packages
if two jars contains same classes (but different class implementation), will they comflict?
Is importing a class same as extending a class?
Object "Savvy"
what is wrong with this Arraylist expression