• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to understand import wildcards

 
Alan Couze
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Chapter 1 it explains that the following import statement is not valid when you want to import java.nio.file.Files and java.nio.file.Paths:



However, I tried creating a couple of test packages and an import such as this seemed to work. For example, I created a Java class



And this code is located in directory test1/test2:



The example above does error out by saying "MyTest.java:1: error: package test1 does not exist". However, if I add any Class to the test1 directory (i.e. "Test1") then the above compiles without error. So, it seems to me that the wildcard for imports will work correctly with sub-packages (i.e. test1.test2) but only if there are Classes in the parent package(s) (test1).

Is this a correct assumption?




 
Paweł Baczyński
Bartender
Posts: 1876
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Package test1 does not exist because you have not put a package statement at the beginning of your Test2 class.

Change it toand see what happens.

You said that your code did not compile. What makes you think anythong works correctly, then?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Couze wrote:In Chapter 1 it explains that the following import statement is not valid when you want to import java.nio.file.Files and java.nio.file.Paths:

That's indeed correct! The wildcard notation will not work with sub-packages.

Alan Couze wrote:However, I tried creating a couple of test packages and an import such as this seemed to work.

Based on your code you are not using any packages at all, because I don't see any package statement in your code. Without a package statement your class will be defined inside the default package. Adding a package statement is very easy

Alan Couze wrote:The example above does error out by saying "MyTest.java:1: error: package test1 does not exist". However, if I add any Class to the test1 directory (i.e. "Test1") then the above compiles without error. So, it seems to me that the wildcard for imports will work correctly with sub-packages (i.e. test1.test2) but only if there are Classes in the parent package(s) (test1).

Is this a correct assumption?

No, it isn't! When you correctly use packages in your code, you'll discover the statement in the study guide is spot-on and a wildcard doesn't work for sub-packages.

Hope it helps!
Kind regards,
Roel
 
Vivek Chulani
Greenhorn
Posts: 6
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I definitely agree with Roel. Adding a package statement (package test1) to the class where Test2 is defined will make it compile.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic