• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trying to understand import wildcards

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?




 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 6
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic