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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compile Issue. Can't Find Class? Its there. 1

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
Hi,

I have two simple programs:

- Box.java : defines a class for a box
- boxtest.java: uses the Box class

I have a directory where I've been compiling java progs:

c:\test>

Here are my steps:

c:\test> javac Box.java ...compiles fine

c:\test> javac boxtest.java
Generates error:


boxtest.java:1:'.' expected
import Box;


Here's are my two separate files. Both of these files are in c:\test>.
----------------

import java.io.*;
class Box
{
//instance variables
double width;
double length;
double height;

//constructors
Box (double w, double l, double h)
{
width = w;
length = l;
height = h;

}

Box (double side)
{
width = side;
length = side;
height = side;
}

//methods
double volume()
{
return width * length * height;
}

double area()
{
return 2 * (width + length + height);

}


}
----------------

import Box;

class boxtest
{
public static void main (String[] args)
{
//Create box with sides 2.5, 3.0, and 5.0
Box box1 = new Box(2.5, 3.0, 5.0);


//Create a box with all sides 3.0
Box box2 = new Box(3.0);

//Show the area/volume for both boxes.
System.out.println("Area for box1 is " + box1.area() + ". Volume for box1 is " + box1.volume());
System.out.println("Area for box2 is " + box2.area() + ". Volume for box2 is " + box2.volume());

}
}

----------------

I've also tried changing the import statement to : import Box.*; but then the compiler looks for a package. What's the problem? I thought we can use classes we create in other programs as long as their in the same directory?

Thanks in advance for your help.
scripter73
Change Your Thinking, Change Your Life.
 
You don't need the import statement if you don't have the two classes in packages. Try removing the import Box; statement and it should work fine.

-gc "I don't look busy because I did it right the first time."
 
just remove the line import Box;

Import is to be used by packages only and Box is not a package.

It compiles and finds all the reference so long as the files are in the same directory! :) Gary Haran
 
AHHH HA HA HA! "I don't look busy because I did it right the first time."
 
Worked like a dream.... Thanks guys!


Change Your Thinking, Change Your Life.
 
You should really declare your classes as public. Classes that aren't declared as public are only visible in the same package. In your case this works since you don'g have a package but this will bite in in the behind at some point and you will probably waste time trying to figure it out.
 
I'll keep a mental note of that. Thanks.

Change Your Thinking, Change Your Life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top