Carlo Salomon
Carlo Salomon

Reputation: 3

declaring a class in java before main method

Sorry for such a simple question but I'm just starting with Java and I haven't been able to solve this problem. I'm copying this code straight from the book but it does not run successfully. I'm using NetBeans. I get the red lights next to the class Vehicle and Class VehicleDemo lines saying "duplicate class: javaapplication3.Vehicle." The file name is JavaApplication3. The output is this:

Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.IncompatibleClassChangeError: javaapplication3.Vehicle and javaapplication3.Vehicle$VehicleDemo disagree on InnerClasses attribute

package javaapplication3;

/**
*
* @author cs
*/


class Vehicle {
  int passengers;
  int fuelcap;
  int mpg;
}

class VehicleDemo {
   public static void main(String[] args) {
      Vehicle minivan = new Vehicle();
      int range;

      minivan.passengers = 7;
      minivan.fuelcap = 16;
      minivan.mpg = 21;

    range = minivan.fuelcap * minivan.mpg;
    System.out.println("Minivan can carry " + minivan.passengers + " with a   range of " + range);                                                            
    }
 }

Upvotes: 0

Views: 1520

Answers (5)

btrballin
btrballin

Reputation: 1464

I believe the problem is the code is incomplete and isn't including all the details for a source code to compile.

Your code seems to be missing a declaration of the actual class, which is the name of the .java file. Suppose your file is name someClass.java, you would need a declaration of that class's body before you implement inner classes. So make sure you have a body like this, assuming the name of the file is someClass.java:

public class someClass{
...
}
class innerClass{

}

Upvotes: 0

Best practice for you from my point of view is that as a beginner you may start with simple text-editor and command-prompt or terminal that could make you to learn more stuffs than working with IDE.

While you following the above method you can always comment the package line(first few lines) which might be there while copying the source. Example: //package *****;

Upvotes: 0

RehanZahoor
RehanZahoor

Reputation: 412

The file name is JavaApplication3

1) The file name has to be VehicleDemo.java.
2) class VehicleDemo { should be public class VehicleDemo {

As your package name is "JavaApplication3" so your folder must be "JavaApplication3", but NetBeans takes care of it, if you have created a project in it and given package name through wizard.

Upvotes: 1

JDK
JDK

Reputation: 83

Give Application name: Application3.java

package com.docmgmt.test;
class Vehicle {

      int passengers;
      int fuelcap;
      int mpg;
    }

class Application3 {

    public static void main(String[] args) {
        Vehicle minivan = new Vehicle();
        int range;

        minivan.passengers = 7;
        minivan.fuelcap = 16;
        minivan.mpg = 21;

        range = minivan.fuelcap * minivan.mpg;
        System.out.println("Minivan can carry " + minivan.passengers
                + " with a   range of " + range);
    }

}

output::

Minivan can carry 7 with a range of 336

Upvotes: 0

Jemuel Elimanco
Jemuel Elimanco

Reputation: 556

remove this line:

package javaapplication3;

or just put // to comment this line

Upvotes: 0

Related Questions