0

I am a newbie in Java, although I have knowledge of Object Oriented Programming from Python but I'm currently having problem understanding this example on creating public class either combining the classes together or in different files and then compile them


public class Vehicle {
  int maxSpeed;
  int wheels;
  String color;
  double fuelCapacity;  

  void horn() {
    System.out.println("Beep!");
  }  
}
class MyClass {
  public static void main(String[ ] args) {
    Vehicle v1 = new Vehicle();
    Vehicle v2 = new Vehicle();
    v1.color = "red";
    v2.horn();
  }
}

The example above was given at Sololearn where I am currently learning, but it only works in Sololearn java compilers. Other compiles throw error

Unable to find static main(String[]) in vehicle

Or main method not found

3
  • 2
    That's not an error the compiler should give afaik. I think you're trying to run the Vehicle class instead of MyClass Commented Jun 10, 2020 at 21:41
  • How are you trying to run your program? Is it in the terminal or in some IDE? Commented Jun 10, 2020 at 21:45
  • Running the program in ide Commented Jun 10, 2020 at 23:04

2 Answers 2

1

First of separate these two classes in different files or make Vehicle class nested in MyClass Nested code will look like this ---

class MyClass {

public class Vehicle {
  int maxSpeed;
  int wheels;
  String color;
  double fuelCapacity;  

  void horn() {
    System.out.println("Beep!");
  }  
}

  public static void main(String[ ] args) {
    Vehicle v1 = new Vehicle();
    Vehicle v2 = new Vehicle();
    v1.color = "red";
    v2.horn();
  }
}

Save this file code as MyClass.java. If you want to run this code you can either use some java editor like eclipse or Netbeans or you can run this via cmd but you should have JDK installed in your system. To know more about it you can ping me any time. Let me know if you are successfully able to run this code. will be happy to help you :)

Sign up to request clarification or add additional context in comments.

8 Comments

Separating two-classes i mean, making a package which contains two files one vehicle.java and other containing MyClass.java both classes in their respected files and importing the vehicle class from that package into MyClass.java file.
Well separating classes is better way and good coding methodology.
The nested-class works well, but please any tip on the other method. Separating the classes and import vehicle class
I have already explained to you that in my first comment. If you have any doubt in that then ping me.
Yes, I saw that but I've spent all day surfing the internet yet i didn't really get how creating / importing user-defined packages work. I can't even find a clear way to do that.
|
1

If you're using Java 11 or higher, make sure that your file is named MyClass.java

In that case you can call it simply by typing in your shell:

$ java MyClass.java

If the java version is less than 11, then you need to compile it first using javac.

2 Comments

The terminal keeps saying javac: not found
Then you need to add java to your PATH. Look here warwick.ac.uk/fac/sci/dcs/people/research/csrcbc/teaching/howto/… (this is rather old, but it's the same for windows 10). Once you've done that, javac will compile your java file to a class file. To run the class file type in java MyClass (don't use .class or .java at the end of the class name).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.