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

imports

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are the following two statements equivalent ?

import  java.util.Arrays.*;

and

import  java.util.Arrays;
 
Enthuware Software Support
Posts: 4447
41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried using Arrays class in your code without the package name using each of these imports?
 
shambhavi sivan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was just trying different code snippets to understand this topic better and came across the following :

this code works fine :

import  java.util.Arrays.*;
public class scrap {
 
 
 
   public static void walk(int start, int... nums) {
System.out.println("helooo !!!");
}

   public static void main(String[] args) {

       Arrays a=new Arrays();
    a.asList();
    System.out.println(java.util.Arrays.asList("one"));
       }
}
   
class Arrays{
   public void  asList()
   {
       System.out.println("asList");
   }
}


but this doesn't :


import  java.util.Arrays;  // shows error
public class scrap {
 
 
 
   public static void walk(int start, int... nums) {
System.out.println("helooo !!!");
}

   public static void main(String[] args) {

       Arrays a=new Arrays();
    a.asList();
    System.out.println(java.util.Arrays.asList("one"));
       }
}
   
class Arrays{
   public void  asList()
   {
       System.out.println("asList");
   }
}


so whats the difference between the 2 ?

 
Paul Anilprem
Enthuware Software Support
Posts: 4447
41
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have too much going on in your code. If you want to see the difference between the two import statements, you should start with something simple:


Use one import statement at a time and see what happens when you compile. Then go through this: http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html
 
shambhavi sivan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No i think i haven't made my question clear . sorry about that .

when i have a scenario as in my two programs in my previous post, I have imported class 'Arrays' from another package java.util as well as created a user defined class 'Arrays' in the same program . So, under such a scenario when i use
import java.util.Arrays.*; i get no error in the program .

but when i use import java.util.Arrays; i get an error .


it is under such a situation when you have same class names, that i want to know the difference between the 2 imports .
i have once again pasted the 2 programs below with the 2 different import statements in each .

NO ERROR :




ERROR :


       
CAN SOMEBODY EXPLAIN .
 
Paul Anilprem
Enthuware Software Support
Posts: 4447
41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I realized now that you are not using static import.

The statement import  java.util.Arrays.*; imports the class * from package java.util.Arrays.
So it is ok to define Arrays class in your code. There is only one Arrays class available in scrap in this case.

The statement import  java.util.Arrays;  imports the class Arrays from package java.util. So now you have two Arrays classes available in scrap code. Compiler realizes that there is ambiguity in the name Arrays so it complains.
 
shambhavi sivan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is class *

and only java.util is a package right ?

java.util.Arrays refers to Arrays class belonging to the package java.util right ?
 
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

shambhavi sivan wrote:what is class *



The * means everything in the package... so...

import java.util.Arrays.*;

means import every class/interface from the java.util.Arrays package. And since that package doesn't exist in your example, the import doesn't actually do anything.

Henry
 
shambhavi sivan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.Arrays cannot be a package because it clashes with the Arrays class in the java.util pakage already provided by java .
i even tried it after reading your message .

so i rephrase my question
what happens when there is an import statement such as :

import package1.sub_package1.classA.* ;

what happens when a wildcard is specified after a class name in the import statement ?
 
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

shambhavi sivan wrote:java.util.Arrays cannot be a package because it clashes with the Arrays class in the java.util pakage already provided by java.



This is *not* true. Remember that a classpath can have multiple directories. So, yes, with the default rt.jar, it will clash, and hence, can't be a package... but you can have a separate directory in the classpath (and also, separate jar files)... ie. it is possible to have that package and not clash.

shambhavi sivan wrote:
what happens when a wildcard is specified after a class name in the import statement ?



As already mentioned, it loads all the classes/interfaces from the package... in this example, the "package1.sub_package1.classA" package.

And also, even if it wasn't possible to not have a clash, how does the first point lead to the second point (your question)? According to the specification, in your example, the package is named "package1.sub_package1.classA". There is nothing in the specification that states, that if there is some class with a conflicting name, then it is not a package.

Oops... This is embarrassing. I completely forgotten about nested classes in this context. Yes, it is possible for the import to succeed, even if only ClassA exists (meaning no package named ClassA).   In this case, the compiler will be trying to import the nested classes of the ClassA class.

Henry
 
shambhavi sivan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh alright henry thank you !
 
I'm not sure if I approve of this interruption. But this tiny ad checks out:
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