Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Importing a class 2

Status
Not open for further replies.

soundmind

Technical User
Jun 30, 2003
16
US
Hi,

I'm just a newbie... trying out some exercises in my Java programming book. It has a number of classes in the accompanying CD but I haven't been able to import them with the exercises I've been following.

Could you please tell me step-by-step how to import a foreign class and use it with a new class?

There is a section on importing classes at the end of the book but it's at the end of the book!

Thank you in advance!
 
Importing is done like "import myOtherClass;" at the of your file. for example, to use the java applet class (this is from the java library) you would have:

import java.applet.Applet;

public class HelloWorldApplet
{
...
...
}

For classes that you made, just make sure those classes lie in the same directory as your main class. If they do not lie in the same directory, you will have to run your program with classpath options which is done on the command line where you execute your program.
 
SolidFish,

Thank you. I tried :

import java.SavitchIn.java;

public class SpeciesFirstTryDemo
{
public static void main(String[] args)
{
}
}

But received an error saying "package SavitchIn does not exist."

All the files are in the same directory. Why is it saying the class is a package?

 
>>>> import java.SavitchIn.java;

This package looks a bit odd - are you sure it is correct ?
The import statement can import a fully-qualified name of class. So :

import java.awt.Component;

The "Component" is the class, which is in the package "java.awt"

Java is case sensitive also ...

Plus the archive (.jar) that contains your class that you wish to import must be on your CLASSPATH. So if your jar was in "C:\java\examples" you would need to type on the console line "set CLASSPATH=%CLASSPATH%;C:\java\examples\myexamplejarfile.jar"

 
Ok, I am assuming the SavitchIn.java is your own class. You will first have to compile this to make it a class.

Do "javac SavitchIn.java"

Once that compiles successfully and creates the "Savitch.class" file, you are ready to import on your other main file. Simply do:

"import SavitchIn"

Make sure you keep the files in the same directory. The reason why we import "java.awt...." files at times is that the "java." indicates a Java Libary class. These classes are installed with your Java JDK. If you are not using the Java Library classes, then you do not need "java.[whatever]"

Hope this helps.
 
A note ...

If he is using sdk1.4, I don't think the compiler supports non-package qualified import statements ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top