• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If main() method is not static Java will throw an exception or an error? (Java OCA 8)

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java OCA 8 Programmer I Study Guide,

1. On the page 7, 3rd paragraph:
Even if a main() method is present, Java will throw an exception if it isn’t static.

exception or error?

2. On page 12, 3rd paragraph, first import statement comment:
import java.nio.*; // NO GOOD – a wildcard only matches class names, not “file.*Files”

Maybe “Files” is stray here, should be “file.*”
 
Paweł Baczyński
Bartender
Posts: 1876
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

>java AAA
Error: Main method is not static in class AAA, please define the main method as:
public static void main(String[] args)


It does print Error but I doubt there is any java.lang.Error being thrown.
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:>java AAA
Error: Main method is not static in class AAA, please define the main method as:
public static void main(String[] args)


I got this error too but I saw it was written "it throws exception" in book. Therefore I want to clear it.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:1. On the page 7, 3rd paragraph:

In this context I'm pretty sure "exception" is not referring to Exception (the parent class of all checked and runtime exceptions), but just to the English noun exception. And because the message you got starts with Error: does not mean that it's an Error (with capital E, as in the subclass of Throwable and the parent class of all classes indicating serious problems that a reasonable application should not try to catch). The message could start with Fatal: as well. Phew, that's a complicated explanation to explain something fairly simple Just remember: the method declaration of the main method must match this method declarationotherwise the application will not run and you'll end up with a crystal-clear exception/error message (telling you what to do).

Mushfiq Mammadov wrote:2. On page 12, 3rd paragraph, first import statement comment:

Correct!
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote: In this context I'm pretty sure "exception" is not referring to Exception (the parent class of all checked and runtime exceptions), but just to the English noun exception. And because the message you got starts with Error: does not mean that it's an Error (with capital E, as in the subclass of Throwable and the parent class of all classes indicating serious problems that a reasonable application should not try to catch). The message could start with Fatal: as well. Phew, that's a complicated explanation to explain something fairly simple Just remember: the method declaration of the main method must match this method declarationotherwise the application will not run and you'll end up with a crystal-clear exception/error message (telling you what to do).


Thanks Roel, it is clear now. The previous sentence confuse me
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.

Throwing error are similar in both case. But it was unclear that it was written error for first case and exception for second case. After your explanation I understand that we can consider exception as error here.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:
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.

Throwing error are similar in both case. But it was unclear that it was written error for first case and exception for second case. After your explanation I understand that we can consider exception as error here.

You'll get indeed a similar message when the main method is missing:
Error: Main method not found in class AAA, please define the main method as:
public static void main(String[] args)


But having an Error and an Exception class in Java, that statement could indeed be confusing (certainly if you are not a native English speaker and you just have learnt about Error and Exception). Maybe it might be (slightly) better to rephrase the 2nd sentence of that statement to: Even if a main() method is present, Java will throw an error as well if it isn’t static.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sticking with this one. The exam creators are also native English speakers and are "sloppy" about exception vs error when it is lowercase. Both mean "something went wrong." It is only when you see the uppercase Error vs Exception that it means the Java definition. If someone is going to be confused, I'd like that person to get it out of his/her system while reading the book than get a question wrong on the exam.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:The exam creators are also native English speakers and are "sloppy" about exception vs error when it is lowercase. Both mean "something went wrong." It is only when you see the uppercase Error vs Exception that it means the Java definition. If someone is going to be confused, I'd like that person to get it out of his/her system while reading the book than get a question wrong on the exam.

I fully agree with this line of thought!
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm also reading this book. Page 7, line no 13. It says
Java will throw an exception if it isn't static
. an exception means as Roel said
the English noun exception
. an exception and Exception of Java differs a lot at least for Java people. Same thing happens when you assign null value to an object reference of a class and try to call the methods of that class then gets java.lang.NullPointerException so that doesn't mean Java supports pointer(The pointer which is in C) or its talking about pointer which we have in C.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:Same thing happens when you assign null value to an object reference of a class and try to call the methods of that class then gets java.lang.NullPointerException so that doesn't mean Java supports pointer(The pointer which is in C) or its talking about pointer which we have in C.

Firstly, I think you are refering to instance methods of a class invoked on a null reference. Because class/static methods can be invoked on a null reference without getting a NullPointerException being thrown. Illustrated in this code snippet
Secondly, although there's a (poorly named) NullPointerException, Java doesn't support pointers. In Java you have reference variables (or references), no pointers. Discussed here as well.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are refering to instance methods of a class invoked on a null reference
yes I meant instance methods of class. because class is only logical so can have static fields and methods. non-static fields and methods belongs to instances. Yes Java won't support pointers that's what I was trying to say.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic