Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: how to import/ access classes created in one package in another package

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    10
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default how to import/ access classes created in one package in another package

    my working directory is e:\ajava. in package p1 i create two classes c1 and c2. net beans creates three files e:\ajava\p1\src\p1, e:\ajava\p1\src\c1, e:\ajava\p1\src\c2. package runs without a hitch. i create another package p2 under e:\ajava. i want to use class c1 in p2. pray what on earth should be my import statement in the p2 source code after the first statement 'package p2'. another related querry what should i include in my class path so as to gain access to c1 and c2 in source code of p2. thanks in advance. ydandurkar


  2. #2
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to import/ access classes created in one package in another package

    Since ajava is the workspace directory, use:
    import p1.*; //to import both c1 and c2, or
    import p1.c1;
    import p1.c2;
    Aceix.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    10
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: how to import/ access classes created in one package in another package

    dear aceix, tried this many times. it does not work.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to import/ access classes created in one package in another package

    What does "does not work" mean?
    Can you show the locations of the files and the contents of the classpath when you try to make it work?
    Also how what packages each of the source files is in.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: how to import/ access classes created in one package in another package

    Just a short paragraph, but I find it quite hard to read due to lack of capitalisation. Please review Capitalization | Punctuation Rules and write with proper capitalisation.

    Rewriting the first part of the post,
    "My working directory is e:\ajava. In package p1 I create two classes, c1 and c2. NetBeans creates three files:
    1. e:\ajava\p1\src\p1
    2. e:\ajava\p1\src\c1
    3. e:\ajava\p1\src\c2

    Package runs without a hitch."

    This doesn't sound right. NetBeans is project-based. Its directory structure is thus

    working_dir\project_name\src\package_name\class_name.java

    (This is at least the way it is with my copy of NetBeans 7.4.)

    Therefore based on the above there is a project called "p1", as well as a package (confusingly) called "p1". Classes c1 and c2 are located in the default package, and not in package p1.

    Continuing on this,
    "I create another package p2 under e:\ajava."

    Is "p2" a package or a project?

    If the resultant directory structure is "e:\ajava\p2\src", then p2 is a project.

    If the directory structure is "e:\ajava\p1\src\p2", then p2 is a package within project p1.

    The answer to the questions on import statement and classpath depends on the clarification of "p2".

  6. #6
    Junior Member
    Join Date
    Apr 2014
    Posts
    10
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: how to import/ access classes created in one package in another package

    dear jashburn, thanks a lot for your detailed comments. sorry for the delay in responding. i was away on assignment. hence i am responding before i reconcile to your remarks. i shall come back to you.

  7. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    10
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: how to import/ access classes created in one package in another package

    Dear Jashburn, I shall be more accurate this time and avoid simplification efforts.

    From NetBeans 7.4 menu File-New Project I created a project with Project Name peft1 and Project Location e:\ajava. It created package peft1 and class Peft1. I added two more classes Point and Edge to package peft1. So I have Projects tab in the IDE showing me peft1-Source Packages-peft1 containing three files Edge.java, Peft1.java, Point.java. And I can also see these three files in the folder E:\ajava\peft1\src\peft1. program works.

    Now I create another project peft2 with Project Name peft2 and Project Location e:\ajava. I wish to use classes Point and Edge created in project and/or package. The first statement in this is 'package peft2;' I am not able to import Point and and Edge classes in peft1. what should be my import statement?

    I have got around the problem by adding project peft1 to Libraries in project peft2 using Add Project menu. peft1-dist/peft1.jar appears along with JDK 1.7 (Default) in Libraries. Then I can simply write 'import peft1.* and use class Point. But this is not what importing is supposed to mean. At least that is my understanding right now. Request clarify.

    I hope I have been clear. ydandurkar

  8. #8
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: how to import/ access classes created in one package in another package

    Your question is clear this time. Well done.

    What you've done by importing project peft1 into project peft2 as a library is actually the right thing to do. This is because Netbeans allows multiple projects to be worked on at the same time without letting code in each of the projects interfere with each other. You can effectively treat code in each of the projects as separate applications.

    Java import statements by default work within a project. In order for it to work across projects, you have to import the other project as a library before code in the other project (peft2) becomes visible to this project (peft1).

    Note that you can have multiple packages within a project. This means there's nothing stopping you from creating package peft2 inside project peft1. Code under package peft1 can then access code under package peft2 using the import statement as both packages now belong to the same application (by virtue of being in the same project.)

    I think the main source of confusion here is, as I've mentioned before, both project and package having the same name, e.g., peft1. Projects are Netbeans artifacts, whereas packages are Java artifacts. If you write your Java code using a plain text editor, the concept of "projects" does not exist.

    I don't know why would package peft1 be created when you create project peft1. Maybe it's a Netbeans setting that causes this to happen. In my copy of Netbeans when I create a new project, it will only contain the default (no name) package. I will then have to manually create any other packages myself, and to avoid the confusion, I wouldn't create a package that has the same name as the project.

  9. The Following User Says Thank You to jashburn For This Useful Post:

    ydandurkar (May 11th, 2014)

  10. #9
    Junior Member
    Join Date
    Apr 2014
    Posts
    10
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: how to import/ access classes created in one package in another package

    Dear Jashburn, thanks a lot.
    You have been a real guru. Very precise and a very fast advise.
    I have a lot to learn from you. Thanks again.
    yd andurkar

Similar Threads

  1. [SOLVED] Which is more efficient? "import java.package.subpackage" or "import java.package.*"
    By Andrew R in forum Java Theory & Questions
    Replies: 1
    Last Post: August 18th, 2013, 01:11 PM
  2. Replies: 4
    Last Post: November 8th, 2012, 07:55 AM
  3. Simple Collision with no Rect package import
    By Dreamer999 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 30th, 2012, 08:45 AM
  4. how to compile java and import it as package
    By clementnas in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 19th, 2011, 05:53 AM
  5. Default Access (package access) confusion
    By gauravrajbehl in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2009, 04:11 AM

Tags for this Thread