how to include built in classes..?

hi friends i am having the doubt in mind,I need to use linked list in my program can i import the linked list class before i use (or) shall i directly include in the code.It means...

import java.util.LinkedList;

class A

{

}

orclass A

{

java.util.LinkedList a=new java.util.LinkedList();

}

I want to knw which one is better...?and anyone tell me the reason...?Thanks in advance

[437 byte] By [83Krisha] at [2007-11-27 10:28:05]
# 1

Neither. You should code to the interface type of List.

cotton.ma at 2007-7-28 17:49:40 > top of Java-index,Java Essentials,Java Programming...
# 2

> I want to knw which one is better...?

There's hardly any difference in the way they work.

aniseeda at 2007-7-28 17:49:40 > top of Java-index,Java Essentials,Java Programming...
# 3

> > I want to knw which one is better...?

>

> There's hardly any difference in the way they work.

Correction. There's NO difference.

jverda at 2007-7-28 17:49:40 > top of Java-index,Java Essentials,Java Programming...
# 4

> > > I want to knw which one is better...?

> >

> > There's hardly any difference in the way they

> work.

>

> Correction. There's NO difference.

I thought so. I was thinking of checking the bytecode to be sure.

aniseeda at 2007-7-28 17:49:40 > top of Java-index,Java Essentials,Java Programming...
# 5

> import java.util.LinkedList;

> class A

> {

> }

> orclass A

> {

> java.util.LinkedList a=new

> java.util.LinkedList();

The .class files generated for the two cases will be identical. Importing has NO effect at runtime. All it does is tell the compiler, "When I say List I mean java.util.List," which saves you some typing and keeps your code less cluttered. Either way, the compiler generates a fully-qualified reference to java.util.List in the class file.

jverda at 2007-7-28 17:49:40 > top of Java-index,Java Essentials,Java Programming...
# 6

> > > I want to knw which one is better...?

> >

> > There's hardly any difference in the way they

> work.

>

> Correction. There's NO difference.

Yes and no.

There is no difference in performance or anything but there is a difference in the source code.

cotton.ma at 2007-7-28 17:49:40 > top of Java-index,Java Essentials,Java Programming...
# 7

> There is no difference in performance or anything but

> there is a difference in the source code.

Um, well, yeah, I guess I figured that was obvious. There's no difference in the .class files though.

jverda at 2007-7-28 17:49:40 > top of Java-index,Java Essentials,Java Programming...
# 8

> > There is no difference in performance or anything

> but

> > there is a difference in the source code.

>

> Um, well, yeah, I guess I figured that was obvious.

Well yes to you and me but we didn't ask the question. :)

Besides this becomes more of an issue if there are multiple List variables used throughout the class.

cotton.ma at 2007-7-28 17:49:40 > top of Java-index,Java Essentials,Java Programming...
# 9

and if there are multiple classes called LinkedList, all of which are possible matches using imports, the first option will fail to compile.

jwentinga at 2007-7-28 17:49:40 > top of Java-index,Java Essentials,Java Programming...