• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Import question

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
To do a great right, do a little wrong - shakepeare. twisted little ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic