-1
//Vehicle.java

import java.util.Random;

class VehicleConstructor{

    private int speed;
    private int timeDriven;
    private int run;
    private int startSpeed; //Pradinis greitis

    public VehicleConstructor() {
        setSpeed();
        System.out.println("Vehicle");
    }

    private void setSpeed(){
        int minSpeed = 1;
        int maxSpeed = 40;

        Random random = new Random();
        this.startSpeed = random.nextInt(maxSpeed - minSpeed + 1) + minSpeed;
    }

    protected int getSpeed(){
        return startSpeed;
    }
}

class Bicycle extends VehicleConstructor{

    public void Bicycle(){
        System.out.println(getSpeed());
    }
}

public class Vehicle{
    public static void main(String[] args){
        Bicycle bicycle = new Bicycle();
    }
}

I want to create object Bicycle which extends VehicleConstructor and call method getSpeed() from parent. The problem is that i get an error:

Error: Main method not found in class Bicycle, please define the main method as: public static void main(String[] args)

As far as i know in Java constructors are with the same name as the class, so i have

 public void Bicycle(){
        System.out.println(getSpeed());
    }

But error says that i need public static void main, and so the other classes also need to be with static, but i don't want this. Is it possible somehow create object without need of static method in it?

Kin
  • 4,466
  • 13
  • 54
  • 106

7 Answers7

3
public void Bicycle(){ //This is a method

public  Bicycle(){ //This is a constructor

Constructor's don't have any return type, not even void.

PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • but it also requires `main` – Kin Dec 27 '12 at 09:19
  • @Kirix seems you are executing Bicycle class alone, run from Vehicle class as it is public and it has a main method .. and also, if all the three class's are in a same file , make sure that the file name is Vehicle. – PermGenError Dec 27 '12 at 09:24
2

The error is self evident.. you dont have a main method, or for some reason, bicycle is defined as entry point when it should not be. After a second glance, looks like your Vehicle should be defined as the entry point, and its not.

p.s your class hierarchy is confusing.

Karthik T
  • 31,456
  • 5
  • 68
  • 87
0

create a separate class having main method to test (without this your program will not execute as it is the starting point) where you can create constructors of your classes

vishal_aim
  • 7,636
  • 1
  • 20
  • 23
0

Your class works fine for me..What is the name of the file in which you have written code?File name should be equal to public class name..

Rajesh
  • 2,934
  • 8
  • 42
  • 71
  • yes, it is: Vehicle.java, but i still receiving error in bicycle class – Kin Dec 27 '12 at 09:22
  • R using eclipse ? I just copy pasted ur code in my eclipse Vehicle.java file and executed it..getting no error..i am running jdk1.6 .. – Rajesh Dec 27 '12 at 09:26
0

I am guessing you are trying to run this class by itself. Java runtime needs that public static void main(String[] args) method when you try to run the class on its own. Try to create another Java class like Test.java that has main function and call the constructor inside it. Be sure to run Test.java otherwise you will keep getting the same error.

Panda
  • 21
  • 3
  • but i am already have `public static void main(String[] args)` in the class `Vehicle` – Kin Dec 27 '12 at 09:26
  • But when try to run it look for that function inside the VehicleConstructor class so it can not run. Take the function outside the Vehicle class, but inside the VehicleConstructor class scope. Your problem is not about the constructor. – Panda Dec 27 '12 at 09:33
0

please , try to seperate the classes into seperates files, and name the files as the name of classes. then you add a public static void main(String[]arg) in the Bicycle class. then run it and you will see the results.

a bouchenafa
  • 272
  • 1
  • 9
  • 21
0

The problem is not at compile time, because even if you compile those files separately, they should compile fine. My guess is you are probably using an IDE (Netbeans or Eclipse) and you are not setting up Vehicle.java as the main class.

Risin
  • 21
  • 3