Page 1 of 1
About Question enthuware.ocajp.i.v7.2.828 :
Posted: Thu Mar 07, 2013 5:54 pm
by Sweetpin2
Since the class is public class TestClass, don't we need a public static void main(String[] args). If yes then public int main(String[] args) in same class will give duplicate method error.
Re: About Question enthuware.ocajp.i.v7.2.828 :
Posted: Thu Mar 07, 2013 7:11 pm
by admin
There doesn't have to be a public static void main(String[] args) in every class. i.e. it is not an error if you don't have this method in a class.
Re: About Question enthuware.ocajp.i.v7.2.828 :
Posted: Sun Jul 05, 2015 3:45 pm
by evafang2008
I think, we can write both standard main() and other version main() in one class, but only be overloaded. If just overloaded main(), no error. Right?
Thanks.
Re: About Question enthuware.ocajp.i.v7.2.828 :
Posted: Sun Jul 05, 2015 8:17 pm
by admin
Yes, right. But you sshould try it out.
Re: About Question enthuware.ocajp.i.v7.2.828 :
Posted: Thu Sep 17, 2015 8:26 am
by troydh53
How are we supposed to know that the main method listed was not the intended standard main method but done wrong? I thought it was intended to be the public static void main(String[] args) method, but they just wrote it wrong to see if i'd catch it.
Re: About Question enthuware.ocajp.i.v7.2.828 :
Posted: Thu Sep 17, 2015 9:08 am
by admin
Unless mentioned otherwise, in this exam, wrong or invalid implies compilation error. In this question (problem statement and options), there is no mention of execution from command line, so you don't really know of the intention. Therefore, all you can see is whether the code will compile or not.
Re: About Question enthuware.ocajp.i.v7.2.828 :
Posted: Wed Aug 19, 2020 11:24 am
by NancyDS
Maybe I should not post this with this question, but this question leaves me confused.
The options to be answered: Which of the lines are invalid?
In this one "public int main(String[] args)" is not to be answered, because: " is a valid declaration of a method named main. Although, it is not a correct declaration for the standard main method that can be used to execute the class, but it is a valid method nevertheless."
I refer to the following other question and answer (enthuware.ocajp.i.v8.2.1080):
question: Which of the following are valid declarations for the standard main method:
wrong option: static void main(String args[])
answer: Although practically correct but for the purpose of this exam you should not select this option because the method is not declared public
and another wrong answer in that same question: public static int main(String args[ ]) {}
This method returns an 'int' instead of 'void'.
My confusion is in the fact that in one question a strict answer is expected but not in the other, can you give me some guidance in how to understand what is expected?
Re: About Question enthuware.ocajp.i.v7.2.828 :
Posted: Wed Aug 19, 2020 4:51 pm
by admin
The distinction is clear:
if the question is asking about the main method that is used to execute the class, pick the one with public static void main(String args[]) or other equivalent variation.
Re: About Question enthuware.ocajp.i.v7.2.828 :
Posted: Sun May 19, 2024 10:06 pm
by pavvel
Why the first option // 1 is incorrect?
Re: About Question enthuware.ocajp.i.v7.2.828 :
Posted: Sun May 19, 2024 10:48 pm
by admin
First option is // 1 and // 4. //1 is valid but //4 is not.
Re: About Question enthuware.ocajp.i.v7.2.828 :
Posted: Sat Aug 31, 2024 9:53 pm
by royjroyj
putting the class Test {} at the top of the file doesn't make it compile since it's lacking a main method. So shouldn't //1 be invalid.
Re: About Question enthuware.ocajp.i.v7.2.828 :
Posted: Sat Aug 31, 2024 11:57 pm
by admin
//1 is valid. It compiles fine. Why do you think it needs a main method? There is no such requirement that every class must have a main method.
Are you using and IDE or any means other than command line to compile and then run the code? See this
https://enthuware.com/oca-ocp-java-cert ... cation-ide