Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Type not found in local declaration  RSS feed

 
Megs Maquito
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Car {
//The car constructor @param efficiency the gas mileage
public Car(double aEfficiency)
{
efficiency = aEfficiency;
}
//Adds gas to a tank @param amount the amount of fuel
public void addGas(double amount)
{
gas = amount;
}
//Drives a car @param distance the distance driven
public void drive(double aDistance)
{
distance = aDistance;
}
//Compute remaining gas after driving a certain distance
public double getGas()
{
double gasLeft = gas - (distance / efficiency);
return gasLeft;
}
private double efficiency, gas, distance;

}


//another class

//to test the Car class
public class TestCar {
public static void main (String[] args)
{
Car exsior = new Car(20);
exsior.addGas(20);
exsior.drive(100);
System.out.println(gasLeft);
}
}

this is the compile error:

TestCar.java:5: Type `Car' not found in the declaration of the local variable `exsior'.
Car exsior = new Car(20);
^
1 error
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch! Let's see if all these things are true:

1 You have two files Car.java and TestCar.java
2 They do not have "package" statements
3 They are in the same directory
4 You cd to that directory before you compile
5 javac Car.java gives no errors
6 javac TestCar.java gives the error

If in fact all of those are right, I'm stumped for the moment. I don't compile from the command line often and may have forgotten some of the details.
 
Megs Maquito
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's is exactly the case, thanks
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a package statement to your classes and recompile. I have a vague recollection that later versions of the JDK do not like the default package which you have.
 
Erik Larson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't worry about a package just yet. Seems like you are just starting in Java and you can worry about that later. You do have a syntax error in the print statement in TestCar. The method gasLeft has no parameters, but the () still need to be there to tell the compiler you are calling a method.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try


and see if that helps. (This is something that you should not have to do, but is likely to help diagnose the problem).
 
Megs Maquito
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for all your help. I finally was able to compile my TestCar.java with:


i also had to change gasLeft to exsior.getGas()

My next question is: Why do I have to include addt'l arguments? Do I have a jdk config error? Thanks again
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Megs Maquito:
Thanks so much for all your help. I finally was able to compile my TestCar.java with:


i also had to change gasLeft to exsior.getGas()

My next question is: Why do I have to include addt'l arguments? Do I have a jdk config error? Thanks again


My guess is that your CLASSPATH variable is set but does not include the current directory. Try this:

For Windows
Type "echo %CLASSPATH%" at the command prompt.

For Unix/Linux:
Type "echo $CLASSPATH" at the command prompt.

Let us know what output this gives so we can help you further.

Layne
 
Megs Maquito
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Layne,

I typed "echo $CLASSPATH" in my terminal and it came out blank.

Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!