• Post Reply Bookmark Topic Watch Topic
  • New Topic

importing class in HeadFirstJava Chapter 5 -SimpleDotComTester.java  RSS feed

 
Jen Jin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


There are two files created, " SimpleDotComTester.java"and "SimpleDotCom.java ",in one project in Eclipse.
"SimpleDotComTester.java " is to test "SimpleDotCom" class, so I would think that it should not be a problem to add in the "SimpleDotComTester.java"file. However, when I add the import statement, I get
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
How so?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Are those two classes in the default package? Meaning it is not in a package?

If so, and if both are in the default package, then there is no need to import. If so, and you want to import it into a class that is in a package, then you can't. Not using a package is generally for testing purposes, and as you noticed, you can't import them.

Henry
 
Jen Jin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are in the default package.

Yes, the book source code does not include " import SimpleDotCom ; " in the "SimpleDotComTester.java". It compiles successfully without the import statement.


But, I thought the import statement should not harm compilation since "SimpleDotComTester.java" is testing an object of "SimpleDotCom" class.


So I don't understand why it has a compilation error upon adding the import statement in the "SimpleDotComTester.java"
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jen Jin wrote:
So I don't understand why it has a compilation error upon adding the import statement in the "SimpleDotComTester.java"


Since I didn't designed Java, or know anyone who did, I can't tell you "why"? All I can say is that you just can't. And interestingly, in older versions of Java, it was actually allowed.

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!