package problem

G

gk

package com.pack1;

import com.pack1.packe2.pack3.*; // is this correct ? its more deeper

import com.*;// is this correct ? its above the package directory

// java class here
 
R

Roedy Green

import com.pack1.packe2.pack3.*; // is this correct ? its more deeper

import com.*;// is this correct ? its above the package directory

It is not a hierarchy even though it looks like one.

You must spell it out longhand.

import com.pack1;
import com.pack2;
 
G

gk

It is not a hierarchy even though it looks like one.

what ? which one is not hierarchy ?

i can save files here "com/pack1/packe2/pack3" folder with a package
statement "package com.pack1.packe2.pack3; " in all my java files.


i can save files here "com" folder with a package statement "package
com; " in all my java files.

but if i have a file with package statement "package com.pack1"
which of the following import work ?

import com.pack1.packe2.pack3.*;
import com.*;

i did not understand what do you mean by hierrarchy
 
G

gk

It is not a hierarchy even though it looks like one.

what ? which one is not hierarchy ?

i can save files here "com/pack1/packe2/pack3" folder with a package
statement "package com.pack1.packe2.pack3; " in all my java files.


i can save files here "com" folder with a package statement "package
com; " in all my java files.

but if i have a file with package statement "package com.pack1"
which of the following import work ?

import com.pack1.packe2.pack3.*;
import com.*;

i did not understand what do you mean by hierrarchy
 
R

Robert Klemme

gk said:
what ? which one is not hierarchy ?

Packages in general.
i can save files here "com/pack1/packe2/pack3" folder with a package
statement "package com.pack1.packe2.pack3; " in all my java files.

This is just a convention used by most Java IDE's and standard class
loaders. But you should remember that packages != directories. You can
place your class files everywhere you like as long as you make sure they
are accessible via some class loader. Of course, there are some drawbacks
in putting all class files in a single directories (size and nameclashes
to name some).

What Roedy tried to convey is that there is no hiearchy of packages
although the dotted notation makes it look like there was. Packages are
just sitting beside each other. package com.foo.bar is not an inner
package to com.foo and has no provileged access to it.
i can save files here "com" folder with a package statement "package
com; " in all my java files.

but if i have a file with package statement "package com.pack1"
which of the following import work ?

import com.pack1.packe2.pack3.*;
import com.*;

None of them. You need

import com.pack1.*;

or

import com.pack1.YourClass;
i did not understand what do you mean by hierrarchy

Hope I could clear this up a bit.

Kind regards

robert
 
G

gk

hi, i am more confused.

is my question clear to you guys ?

here i am reposting it again




code 1:
--------
package com.pack1.packe2.pack3;

class klass1
{
// code

}

this code has been saved in com/pack1/packe2/pack3 directory.



code 2:
--------

package com;

class klass2
{
// code

}

this code has been saved in com directory.













code 3:
--------

package com.pack1;

import com.pack1.packe2.pack3.*; // is this OK
import com.*; // // is this OK

class MyClass
{
// i want to use klass1 and klass2 here. so, how do i import those
classes now ? is now import statement correct ?

}

this code has been saved in com/pack1 directory.
 
A

Alan Krueger

gk said:
package com.pack1;

import com.pack1.packe2.pack3.*; // is this correct ? its more deeper

import com.*;// is this correct ? its above the package directory

// java class here

It's not clear what you're asking. The import of com.* will only import
classes directly in package com, it won't import anything deeper.

Many believe you shouldn't import using *, that you should import only
the classes you need. Eclipse generally supports this by adding imports
as needed and by removing any unnecessary ones when you organize your
imports.
 
C

Chris Smith

gk said:
package com.pack1;

import com.pack1.packe2.pack3.*; // is this OK
import com.*; // // is this OK

class MyClass
{
// i want to use klass1 and klass2 here. so, how do i import those
classes now ? is now import statement correct ?

}

this code has been saved in com/pack1 directory.

Yep, you're fine.

(If you want additional nitpicking, it's a good idea to avoid wildcard
imports because they can lead to compatibility problems in later code.
However, wildcards do work.)

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top