Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: class [class name] is public, should be declared in a file names [class name].java

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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();
    }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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)

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    jimmypdiddy (June 17th, 2012)

  4. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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;

  5. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: class [class name] is public, should be declared in a file names [class name].jav

    I think I figured it would.

Similar Threads

  1. get method names in all the class files in the jar/war/ear
    By purushothaman in forum Java SE APIs
    Replies: 0
    Last Post: March 20th, 2012, 08:45 AM
  2. How can you get the compiler to run more than one public class?
    By tai8 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 19th, 2012, 04:27 PM
  3. What's difference between public class and abstract class?
    By Java95 in forum Java Theory & Questions
    Replies: 7
    Last Post: January 24th, 2012, 07:37 AM
  4. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 10:40 PM
  5. Public class help/error
    By Plural in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 11th, 2010, 05:22 PM

Tags for this Thread