Question:
Importing packages when writing java programs.?
wackydude33
2010-02-10 13:02:29 UTC
I wrote a class in java called Mass that had the method public static double atom(double atomicMass). I tried importing the class into another program and used the method Mass.atom(atomicMass). When i tried compiling the program i kept getting an error saying "cannot find symbol" and it pointed to the M on my method. How do i fix this?
I think i may have imported it wrong cuz i'm not 2 sure how 2 do that. The class is in the folder C:\Users\Brenden\Documents\My Received Files\CSE1020
What should my import statemenet look like?
Three answers:
Mark aka jack573
2010-02-14 09:09:06 UTC
I will assume that you have not put these files into a package with a line like this:

package mypackage.myclasses;



1)

If you have not put them into packages, AND the Mass.class file is in the same folder as your other class, you do not need to have an import statement for this class to be found.



2)

If you did put it in a package AND the Mass.class file is in the same package, then again, you do not need an import statement.



3)

If you have put it in a package AND the Mass.class in is another folder, then you will need an import statement declaring the entire package name with the class. So iusing he example above, you would have:

import mypackage.myclasses.Mass;

You would also need to put the folder what mypackage is in on the classpath so Java could find the mypackage folder.



If you want to have an import statement when 1) or 2) is true, you could have this:

import Mass

but it is totally unnecessary to do so.



Oh, there is one other posibility. If the Mass.class file is not in the same folder as your class file, then you would need to put the folder where the Mass.class file is on the classpath. If it is in a package, you would need to import mypackage.myclasses.Mass; statement.



Hope that helps.
Susan
2016-04-10 22:21:45 UTC
You need import alice.filename; at the start of your program in order to use the classes in your package. Get a book on Java - it will all be in there.
Neunerball
2010-02-10 14:26:37 UTC
check the tutorial at:

http://java.sun.com/docs/books/tutorial/java/package/index.html


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...