Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Override the main method

 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got confused about the this question in Enthuware test 6. I thought the answer is "print nothing" because I remember it is fine to override the main methods. Why an error would be thrown here?
overridemain.png
[Thumbnail for overridemain.png]
 
Roel De Nijs
Sheriff
Posts: 10763
148
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nemo zou wrote:I thought the answer is "print nothing" because I remember it is fine to override the main methods. Why an error would be thrown here?

First of all, that's not an override! Only inherited instance methods can be overridden.

Secondly, you'll get an error because the JVM is looking for a method public static void main (String[]) - exactly. So because the return type is long, the JVM can't find the main method to run your application and you'll get something like:
Error: Main method must return a value of type void in class TestClass, please define the main method as:
public static void main(String[] args)


Hope it helps!
Kind regards,
Roel
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!