class [class name] is public, should be declared in a file names [class name].java
Hello,
I am new to programming and struggling through trying to learn. Everything was going well, then I encountered the following error:
class [class name] is public, should be declared in a file names [class name].java
Here is the code I am working with:
class Vehicles {
public static void main(String args[]) {
Vehicle minivan = new Vehicle();
Vehicle sportscar = new Vehicle();
int range1, range2;
// assign values to fields in minivan
minivan.passengers = 7;
minivan.fuelcap = 16;
minivan.mpg = 21;
// assign values to fields in sportscar
sportscar.passengers = 2;
sportscar.fuelcap = 15;
sportscar.mpg = 28;
System.out.println("Minivan can carry " + minivan.passengers + ".");
minivan.range();
System.out.print.ln("Sportscar can carry " + sportscar.passengers + ".");
sportscar.range();
}
}
Re: class [class name] is public, should be declared in a file names [class name].jav
That code declares a class Vehicles, but within it you refer to another class: Vehicle.
I suspect you only intend there to be one class. In any case the name of the public class *must* match the file name. (Vehicle in Vehicle.java, or Vehicles in Vehicles.java)
Re: class [class name] is public, should be declared in a file names [class name].jav
Thanks for the reply. That got me past that error, now I have a bunch of others. Basically it is saying that it cannot find the symbol '.' in the statements such as minivan.passengers = 7;
Re: class [class name] is public, should be declared in a file names [class name].jav
I think I figured it would.