Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

package/import Statements in Kathy's book

 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Can any one tell me whether the following package and import statements work?
This is from Kathy's Book Page 106. Observe that we are importing Foo class from wickedlysmart and then declaring another class with same name.
import java.util.*;
import.com.wickedlysmart.Foo;
class Foo{}

I created a calss Test in xxx package and then when imporing the above class in another package and try to declare class Test I am getting compiler error.
Here is the code I am using.
// I could compile this without any problem.
// this code is saved in F:\xxx\Test.java
package xxx;
public class Test {
public static void main(String[] args) {
Long l= new Long(5);
Integer i= new Integer(5);
System.out.println(l.equals(i));
}
}
// This fail to compile
// This is in F:\yyy\Test.java
package yyy;
import xxx.Test;
class Test{}
Here is the error I am getting..
F:\yyy>javac Test.java
Test.java:2: yyy.Test is already defined in this compilation unit
import xxx.Test;
^
1 error
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This error will always happen when you have 2 equal names in one unit, to solve,
class xxx.Test may be:
package yyy;
import xxx.*;// I modified this line
class Test{}
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sarma -
Sorry ! You found an error in the book - this has already been corrected, you'll find the correction on the errata page at :
web page
We just changed the class names to 'Bob'
-Bert :roll:
[ March 01, 2003: Message edited by: Bert Bates ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic