Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Sybex 815 - potential error - chapter 1

 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
book: Oracle Certified Professional Java SE 11 Programmer I Study Guide Exam 1Z0-815

chapter: 1

If a main() method isn’t present in the class we name with the .java executable, the process will throw an error and terminate. Even if a main() method is present, Java will throw an exception if it isn’t static.



but if the main method is not marked as static Java will also throw an error, not an exception

method main non-static:
Error: Main method is not static in class Main, please define the main method as:
  public static void main(String[] args)
 
Master Rancher
Posts: 1011
28
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Confirmed that whether the main() method is completely missing, has the wrong parameters, or is not marked static, you get the Error:
Error: Main method not found in class Eraseme, please define the main method as:
  public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application


If you try to return a value from it, because you are used to C/C++, you will get:

Error: Main method must return a value of type void in class Eraseme, please
define the main method as:
  public static void main(String[] args)


Still an Error, not an Exception.

It doesn't get capitalized if we use the newer run-straight-from-source approach:

PS F:\Java> java Eraseme.java
error: can't find main(String[]) method in class: Eraseme

PS F:\Java> java Eraseme.java
error: 'main' method is not declared with a return type of 'void'

PS F:\Java> java Eraseme.java
error: 'main' method is not declared 'public static'


But in all cases is indeed an error.

The exams are super-tricky, and it can be very annoying.

There is a lot of focus on whether something will fail to compile or throw an Exception or Error at runtime.

I think we are good for the exam in this particular case knowing that it will compile but fail to run.

You definitely want to know all the difference between Throwable, Error and Exception, as well as being able to clearly distinguish the difference in rules for handling sub-classes of RuntimeException versus sub-classes of Exception that are not sub-classes of RuntimeException for the exam.

This shows a good eye for detail, but I think of all the places where whether something is an Error or an Exception makes any difference, this one is least likely to show on an exam.  You'll see plenty more that make some more difference thru-out these long books.
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic