package problem

Discussion in 'Java' started by gk, Jan 11, 2006.

  1. gk

    gk Guest

    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
    gk, Jan 11, 2006
    #1
    1. Advertising

  2. gk

    Roedy Green Guest

    On 10 Jan 2006 23:03:11 -0800, "gk" <> wrote, quoted
    or indirectly quoted someone who said :

    >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;
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 11, 2006
    #2
    1. Advertising

  3. gk

    gk Guest

    >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
    gk, Jan 11, 2006
    #3
  4. gk

    gk Guest

    >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
    gk, Jan 11, 2006
    #4
  5. gk wrote:
    >> It is not a hierarchy even though it looks like one.

    >
    > 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
    Robert Klemme, Jan 11, 2006
    #5
  6. gk

    gk Guest

    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.
    gk, Jan 11, 2006
    #6
  7. gk

    Alan Krueger Guest

    gk wrote:
    > 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.
    Alan Krueger, Jan 11, 2006
    #7
  8. gk

    gk Guest

    look at my example code.

    is that posible ?
    gk, Jan 11, 2006
    #8
  9. gk wrote:
    > look at my example code.
    >
    > is that posible ?


    Did *you* actually look at the explanations given?

    robert
    Robert Klemme, Jan 11, 2006
    #9
  10. gk

    Chris Smith Guest

    gk <> wrote:
    [...]
    > 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
    Chris Smith, Jan 11, 2006
    #10
  11. gk

    Chris Smith Guest

    Robert Klemme <> wrote:
    > gk wrote:
    > > look at my example code.
    > >
    > > is that posible ?

    >
    > Did *you* actually look at the explanations given?


    To be fair, they weren't very good.

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jan 11, 2006
    #11
  12. gk

    Alan Krueger Guest

    Chris Smith wrote:
    > To be fair, they weren't very good.


    To be fair, neither was the original question. It's hard to answer
    something that isn't clear.
    Alan Krueger, Jan 12, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Parvinder
    Replies:
    6
    Views:
    730
    Thomas G. Marshall
    Feb 27, 2005
  2. Dave
    Replies:
    2
    Views:
    453
  3. George P
    Replies:
    3
    Views:
    669
    Alex Martelli
    Sep 11, 2004
  4. David Pratt
    Replies:
    4
    Views:
    324
    David Pratt
    May 13, 2006
  5. Replies:
    1
    Views:
    15,050
    drobert_bfm
    Nov 27, 2007
Loading...

Share This Page