-3

I am trying out an association example in java; when compiling, it gives the error message saying "java cannot find main in CarClass". I double checked the "main" syntax, tried multiple versions - still doesn't work. Appreciate any help!

class CarClass{
   String carName;
   int carId;
   CarClass(String name, int id)
   {
    this.carName = name;
    this.carId = id;
   }
}
class Driver extends CarClass{
   String driverName;
   Driver(String name, String cname, int cid){
    super(cname, cid);
    this.driverName=name;
   }
}
class TransportCompany{
   public static void main(String args[])
   {
    Driver obj = new Driver("Andy", "Ford", 9988);
    System.out.println(obj.driverName+" is a driver of car Id: "+obj.carId);
   }
}

Thanks.

IT newbie
  • 3
  • 1
  • That kind of error is a runtime error, not a compilation error. You need to tell us, precisely, the sequence of commands (and directories) you use to compile and run that class. But indeed, if you read the error message and the class source code, you should realize that there is no main method in CarClass. The main method is in TransportCompany. – JB Nizet Sep 08 '18 at 10:04
  • Your code is working fine for me. Please elaborate on the error. – driftking9987 Sep 08 '18 at 10:05
  • Define `TransportCompany` as public – Nicholas K Sep 08 '18 at 10:10
  • @JBNizet you are right. It is a run time error that says "cannot find 'main' method in CarClass". The part I don't get is: doesn't me creating object invoke constructor in CarClass? If it does, why is it not working? I think the code is straightforward enough. – IT newbie Sep 08 '18 at 10:12
  • @NicholasK don't think I can do that. The whole point is to set CarClass and public and somehow invoke the base code through the other two classes. Though should I add some code and tie TransportCompany with CarClass? – IT newbie Sep 08 '18 at 10:15
  • Are all the 3 classes you defined in the same .java file? – Nicholas K Sep 08 '18 at 10:17
  • @NicholasK yes absolutely. I checked the .java and .class file name. All seems right. – IT newbie Sep 08 '18 at 10:18
  • That is a bad practice, any reason why you are doing that? – Nicholas K Sep 08 '18 at 10:20
  • @NicholasK what is bad practice? keeping different classes under same file? wasn't sure what you are referring to... – IT newbie Sep 08 '18 at 10:24
  • Yes it is a very bad practice. Each class should be defined in its own file. For eg: How would someone else know if you created 3 classes all in 1 file? It reduces code readability. – Nicholas K Sep 08 '18 at 10:27
  • Also not really sure why you reverted your decision of accepting my answer :/ – Nicholas K Sep 08 '18 at 10:28
  • @NicholasK Noted :) I am new to Java and here... didn't know you could only accept one answer... – IT newbie Sep 08 '18 at 10:31

2 Answers2

0

you should have file name same as the class name containing main method.

And also make class containing main() method as public

ex: Demo.java

public class Demo{
public static void main(String args[]){
......
}
}

in your case TransportCompany.java

class CarClass{
   String carName;
   int carId;
   CarClass(String name, int id)
   {
    this.carName = name;
    this.carId = id;
   }
}
class Driver extends CarClass{
   String driverName;
   Driver(String name, String cname, int cid){
    super(cname, cid);
    this.driverName=name;
   }
}
public class TransportCompany{
   public static void main(String args[])
   {
    Driver obj = new Driver("Andy", "Ford", 9988);
    System.out.println(obj.driverName+" is a driver of car Id: "+obj.carId);
   }
}
spa
  • 329
  • 1
  • 3
  • 15
0

If all your classes (CarClass, Driver & TransportCompany) are all in the same .java file, the class that contains the main method and the file name should be the same.

In your case, add the keyword public to TransportCompany - that way the JVM knows to look for the main method in TransportCompany

Nicholas K
  • 15,148
  • 7
  • 31
  • 57