Do I need to import a class if it is in the project of the class I need it for?

Posted on 2005-04-16
Last Modified: 2012-05-05
I have 3 files in my project.  


I need the file to use some of the functionality of the file.  Do I need to import it?  I have tried to import it but I am not sure of the proper syntax to use.  And I my Pod file does not seem to acknowledge that the functionality of the file.  

Thank you Experts,

Question by:travishaberman

    Author Comment

    OK.. I think I have a part of this figured out...  The I am trying to preform an operation on an object in a Vector container.  However, the container is initially given no objects so it does not know the type of the objects it contains.  The end result is that is that the compiler does not know what type of object I am trying to access so it tells me that it cant find the method .  So, I think I can fix this if I can tell the compiler that the object is of type "Morg".  That is, I need to tell the compiler that I am initializing a Vector of Morg objects.  Does anyone know how to do that?

    LVL 9

    Accepted Solution


    Is it in the package or same class file?

    If it is in the package, you could just use

    packagename.class object = new packagename.class()

    If you don't need to istantiate it, then you could just do this.


    Now if it is in the same class file, is it considered to be a different classname, if it is then you will need to reference it with the first method. Or import it to use just
    Classname = new Classname()

    LVL 25

    Assisted Solution

    If it's in the same directory, then you can do this:

       Morg m = new Morg();
       // use the 'm' object here, for example: m.method();

    Also, as cyberdevil67 said, if there's no constructor in the Morg class, and thus you need not instantiate it, you can just use this:


    (that's once again, assuming they're all in the same directory).
    LVL 16

    Assisted Solution


    Well, as what cyberdevil67 and InteractiveMind suggest you, you may not declare the import statement if they are within the same directory but for the best practice, it is wise for you to always import them. It will help you in maintaning your codes. Believe me :)!.

    Well, what you can do is that if your method returns object instead of Vector, you can "cast" it into Vector such as (Vector)(obj);

    Please let me know if you need additional information.

    Best Regards
    LVL 25

    Expert Comment

    A split seems fair enough. I would however, for future users, like to point out that with my comment about accessing a method like so:


    The method() must also be static! (And in many cases, have an Access Control level of "public").

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now