Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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
8
Medium Priority
?
249 Views
Last Modified: 2012-05-05
I have 3 files in my project.  

1) main.java
2) Morg.java
3) Pod.java

I need the Pod.java file to use some of the functionality of the Morg.java 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 Morg.java file.  

Thank you Experts,

-TH
0
Comment
Question by:travishaberman
5 Comments
 

Author Comment

by:travishaberman
ID: 13799200
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?

-TH
0
 
LVL 9

Accepted Solution

by:
cyberdevil67 earned 672 total points
ID: 13799929
Ok,

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.

packagename.class.methodname()

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()

0
 
LVL 25

Assisted Solution

by:InteractiveMind
InteractiveMind earned 664 total points
ID: 13800811
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:

   Morg.method();

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

Assisted Solution

by:suprapto45
suprapto45 earned 664 total points
ID: 13801394
Hi,

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
Dave
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 14219193
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:

   Morg.method();

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

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
Scripts are great for performing batch jobs against users, however sometimes the GUI is all you need.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

581 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