• Post Reply Bookmark Topic Watch Topic
  • New Topic

which way to import  RSS feed

 
Pratibha Nandwana
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
when we write import statemnets should we import only the specific class we want as import package.<the specific class which is to be imported>
or shuld we do import packagename.* ? In latter case will all classes get
imported..Is there any difference between two in case i want only 1 class then which is better?
 
Jean-Sebastien Abella
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is always better to be more specific. This way when someone open the files he knows what object are used(instead of finding it out).

I'm not sure but I think it can be faster on the compilation.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of us prefer the specific class approach because it documents things better and avoids potential collisions if two packages have the same class name.

If you're using an IDE like Eclipse, you can import * while you're thinking and coding and use right-click, Source, Organize Imports (or ctrl-shift-o) to fix them up later.
 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pratibha Nandwana:
Hi
when we write import statemnets should we import only the specific class we want as import package.<the specific class which is to be imported>
or shuld we do import packagename.* ? In latter case will all classes get
imported..Is there any difference between two in case i want only 1 class then which is better?


Hi Pratibha,
Well, basically it all depends on what your program requirements are.
In case you are going to work with random classes from a specified package, you can specify the package name.

However, if you plan to work with just 2-3 classes, it is better to import the specific classes as pointed by another poster as that makes things easier and tidier.

But in the end, I personally feel that importing all classes is a good idea if the number of classes you are going to work with is less than 5. No particular reason for that...just tidier and simpler to debug code !!

Your choice.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

Its better to be more specific....ensures more readability of code.

but whether u r specific or include all the classes with a *..

the difference exists only during the compilation...

During compilation.... * ...would make the compilation process to consider

all the classes....though most of them are not used...

but during runtime...only the required and used classes are part of

xcution....as provided by the environment..

So basically at runtime...it doesnt make much of a difference... whether u

use a * or ur specific.


Tx
 
Pratibha Nandwana
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..
 
Ritika Pathak
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pratibha

Most of the times it is better to be specific, however it is primarily a question of style.

It makes no difference in generated class files since the generated class file references the classes that are actually used.

There is a practical benefit to import single classes, when two or more packages have classes with the same name then a compilation error might occur.

eg. import java.util.*
import javax.swing.*

This will give you a error while compiling because both have a class called Timer.

Cheers!!
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A comment on the import statement.
Import is nothing like C's include statement. The include statement actually brings source code into your source program at compile time.
I think of import as an extension to the classpath. It gives the compiler a path to use when searching for class definitions. It doesn't bring any code into the source.
 
Megs Maquito
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i also read somewhere that it provides better security for you class if you import only specific classes into your class. That is, the other classes of the same package will not be used.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!