Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3303792
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:59:39+00:00 2026-05-17T20:59:39+00:00

Can same class exist in multiple packages? In other words, can I have Foo.java

  • 0

Can same class exist in multiple packages?
In other words, can I have Foo.java class in com.test.package1 and com.test.package2?

Update

Now I copied class from package 1 and placed in to package 2 and now I am creating an instance of that class, I want this instance to point to class present in package 1 but currently it points to package1 path, how can i modify it?

Oh so I cannot do something like:

Foo = new Foo() // pointing to Foo class in package 1
Foo = new Foo() // pointing to Foo class in package 2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:59:39+00:00Added an answer on May 17, 2026 at 8:59 pm

    Yes, you can have two classes with the same name in multiple packages. However, you can’t import both classes in the same file using two import statements. You’ll have to fully qualify one of the class names if you really need to reference both of them.


    Example: Suppose you have

    pkg1/SomeClass.java

    package pkg1;
    public class SomeClass {
    }
    

    pkg2/SomeClass.java

    package pkg2;
    public class SomeClass {
    }
    

    and Main.java

    import pkg1.SomeClass;   // This will...
    import pkg2.SomeClass;   // ...fail
    
    public class Main {
        public static void main(String args[]) {
            new SomeClass();
        }
    }
    

    If you try to compile, you’ll get:

    $ javac Main.java
    Main.java:2: pkg1.SomeClass is already defined in a single-type import
    import pkg2.SomeClass;
    ^
    1 error
    

    This however does compile:

    import pkg1.SomeClass;
    
    public class Main {
    
        public static void main(String args[]) {
            new SomeClass();
            new pkg2.SomeClass();   // <-- not imported.
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class SomeClass, which can populate itself from a datarow in it's
How can I test the same regex against different regular expression engines?
Can you use the bash getopts function twice in the same script? I have
How can I open multiple Eclipse workspaces at the same time on the Mac?
I have a many-to-many relationship between photos and tags: A photo can have multiple
I declared a private variable vector<SomeClass> theVector; someplace inside my SomeClass class. Why can't
While for the most part you can generate the same MSIL, there are definitely
Can two domain objects show on the same page, when the list method is
Can a cookie be shared between two sites on the same top level domain?
How can I set parent-child relation on same table? Id int, title string, ParentId

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.