Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Import question

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm wondering why I wouldn't want to import java.util.* if I needed at least one of the classes it contains. In other words, why import java.util.MyClassIneed; instead of just importing java.util.*;. Does it really make any difference? Where?
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes a difference and it doesn't.
Maybe not in your learning effort but it is better programming practice to import only the classes that you need instead of the "blanket import" that ".*" allows.
That's because in more complex apps, you want to avoid "clashes" with names of classes that may have the same name but belong to different packages.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It also makes it easier for someone maybe not as familar with the class structure to see where the classes you are using are coming from, if you explicitly name each class in your import statements.
 
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well using the * or the specific class will make a slight difference at the compile time and not at runtime.
When you are using *, during compilation the compiler tries to find the specific class whose function you are using and it then replaces * with classname. SO the class file doesn't contain any *. This option is given for the programmers convenience so that if he is using a number of classes from a package he need not specify import all the classes explicitly.
To understand better generate the class file for the given source file and check through some decompiler what changes the compiler has made.
source file - Test3.java
import java.util.*;
public class Test3
{
public static void main(String[] args)
{
Date dd = new Date();
System.out.println(dd.getDate());
StringTokenizer st = new StringTokenizer("this is a test");
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
}

}
}
class file - Test3.class viewed through decafe (decompiler)
import java.util.*;
public class Test3
{
public static void main(String[] args)
{
Date dd = new Date();
System.out.println(dd.getDate());
StringTokenizer st = new StringTokenizer("this is a test");
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
}

}
}
I hope you must have understood it.
Parmeet
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic