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 5 of 5

Thread: Using methods of one package in another package in same application? To import or not?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Using methods of one package in another package in same application? To import or not?

    Salaam all!

    I have an interface declared as "public" in a package called "package1". I want to use its methods in another package "package2" so do I have to import the package1 in the package2 ? both are in the same application


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Using methods of one package in another package in same application? To import or not?

    What happened when you tried without the import? Was there an error or did it compile?

  3. The Following User Says Thank You to jps For This Useful Post:

    shaz (November 8th, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Using methods of one package in another package in same application? To import or not?

    Yes we need to import. Only when its imported then comes the part whether it can be accessible depending on the visibility(private, public, protected)

  5. The Following User Says Thank You to techiepro For This Useful Post:

    shaz (November 8th, 2012)

  6. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Using methods of one package in another package in same application? To import or not?

    Yes without importing the netbeans editor shows a message that "add import for package1.<interface name>"

    --- Update ---

    but I read that with default modifier( no modifier) we can only access an interface in the same package but with public it can be accessed by any package? Can you explain this please

  7. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Using methods of one package in another package in same application? To import or not?

    Sometimes there arises a need when a class that needs to be accessed from within the same package then default modifier is used.
    For example : a third party library/jar which has a class used by other classes only within that package and it does not wants the consumers/users to use it.

Similar Threads

  1. Simple Collision with no Rect package import
    By Dreamer999 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 30th, 2012, 09:45 AM
  2. Replies: 1
    Last Post: August 5th, 2012, 06:01 AM
  3. Simple package problem, package does not exist error
    By Farmer in forum Object Oriented Programming
    Replies: 3
    Last Post: August 23rd, 2011, 12:18 PM
  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, 06:53 AM
  5. Package help for a newbie
    By Skunkle in forum Java Theory & Questions
    Replies: 3
    Last Post: April 15th, 2011, 02:09 PM