Win a copy of Spark in Action this week in the Open Source Projects forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

wildcards

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to use the asList() method of Arrays class i give
import java.util.Arrays;

but if i give java.util.Arrays.*;
no error is shown
what actually gets imported here ?  
 
Sheriff
Posts: 7635
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Something isn't correct with your question.
Reading your post I understand that first way of importing gives you an error, as you explicitly state that second one doesn't.
Actually it is other way round in practice. Please clarify what you mean.
 
author
Posts: 23877
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Interestingly, I had answered a similar question to this yesterday.  And in that topic, I had assumed that the OP created a package in the classpath (either as a directory or in a jar file). In thinking about it some more, it was not a good assumption -- and I had to go back to elaborate on the answer.

Liutauras Vilda wrote:
Something isn't correct with your question.
Reading your post I understand that first way of importing gives you an error, as you explicitly state that second one doesn't.
Actually it is other way round in practice. Please clarify what you mean.



This time... assuming the opposite, in that the OP did *not* create a package in the classpath ( ), the answer is that neither import should generate an compile error related to the import itself.

An "import java.util.Arrays" should work, as that should import the java.util.Arrays class, from the core Java library.  And an "import java.util.Arrays.*" should also work, as that should import the nested classes, from the java.util.Arrays class, from the core Java library.  Additionally, while that class probably do not have any public nested classes, according to the JLS, importing (on demand) a class with no nested classes (or a package with no classes), is not considered a compile error.

Henry
 
Liutauras Vilda
Sheriff
Posts: 7635
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see Henry. I'm a bit differently interpreted OP's question. Not sure if correctly after all.

OP specifically stated about using asList() method, which is static and usually being referred by the class itself.
So in my head I was thinking about something like:
which requires importing of java.util.Arrays and java.util.Arrays.* would prevent code from compiling, but not because of presence of java.util.Arrays.*, but because of not presence of java.util.Arrays (assuming op importing one of those only).

And now I'm not sure actually what OP is asking, hence requires clarification
 
Liutauras Vilda
Sheriff
Posts: 7635
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ruby rose wrote:but if i give java.util.Arrays.*;

And taking a second look, OP might actually meant java.util.*, which kind of would be on topic too.

@OP
That is why always is a good idea to provide some actual examples, so would be brutally clear what you mean.
 
rochelle roger
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends !

I'm extremely sorry for the confusion my question created and for my delayed reply !

What i actually meant was :

i have read that the import statement only imports classes and interfaces from the specified package and not any other sub packages or anything else .
So i guessed that a wildcard after the class name 'Arrays' should have thrown an error since its not  a package that holds classes or interfaces.
But contrary to my expectations, it didn't !
So i wanted to know what exactly does this statement import, in that case ! java.util.Arrays.*;

Hope i'm clear now !  
 
Henry Wong
author
Posts: 23877
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

rochelle roger wrote:
So i wanted to know what exactly does this statement import, in that case ! java.util.Arrays.*;



As mentioned in a previous post, this is doing an on-demand import of the nested classes that are in the java.util.Arrays class.  As a side note, I don't think the java.util.Arrays class has any public nested classes to be imported, but according to the JLS, that shouldn't generate a compile error.

Henry
 
rochelle roger
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh okay thanks henry ! thanks everybody for helping me !      
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic