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.
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
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.
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?