About Question enthuware.ocajp.i.v7.2.1069 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1069 :
Answer requires for us to select 2 options but isn't option 1 right also?
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1069 :
Did you read the detailed explanation?
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Wed Apr 17, 2013 6:35 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1069 :
The explanation for Option 1 says :admin wrote:Did you read the detailed explanation?
"Surprisingly, it does work. Even if the class is defined in a package."
I just tried it and it does indeed work.
So this should be a valid answer also
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1069 :
Please read all the explanation provided with the question. It also says,smearaDubha wrote:The explanation for Option 1 says :admin wrote:Did you read the detailed explanation?
"Surprisingly, it does work. Even if the class is defined in a package."
I just tried it and it does indeed work.
So this should be a valid answer also
However, for the purpose of Java Certification exam, it should be assumed that for the JVM to execute a class using the standard main method, the accessibility of the main method must be public.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1069 :
During the exam, should I always expect that the main method is public? and wrong if not? Please advice.However, for the purpose of Java Certification exam, it should be assumed that for the JVM to execute a class using the standard main method, the accessibility of the main method must be public.
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1069 :
If the main method is not public, you should not expect the class containing that method to execute from command line. So in that respect, it is "wrong". But there is nothing wrong with the method as such. It is as good as any other method.
If you like our products and services, please help us by posting your review here.
-
- Posts: 26
- Joined: Wed Oct 02, 2019 1:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1069 :
In the book, section 12.3.3, it says the order is
And it says that if you ever get confused, you can use a final version of the main method to figure it out:
I specifically made an effort to create a mnemonic for this ("Prominent Superhero Fights Villainous Menace," if anyone cares).
However, this particular question lists
as a correct answer, even though "final" is in the wrong place.
Both versions seem to compile and run fine, so now I'm wondering why the book emphasizes this specific order if it doesn't actually matter?
Code: Select all
<access modifier> <static> <final or abstract> <return type> methodName(<parameter list>)
Code: Select all
public static final void main(String[] args)
However, this particular question lists
Code: Select all
final public static void main(String [ ] array)
Both versions seem to compile and run fine, so now I'm wondering why the book emphasizes this specific order if it doesn't actually matter?
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1069 :
The book also says this before talking about the order:
The book should make this clear to avoid confusion.
For compilation to succeed, the only requirement is that the return type should be present just before the method name. Order of the other keywords is only by convention.The exam, however, does not try to trick you on the order
of access modifiers and final and abstract keywords.
The book should make this clear to avoid confusion.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Sat Jun 15, 2019 12:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1069 :
The explanation for the option "final public static void main(String [ ] array)" says:
This confuses me because I know and I've tried that final static or non-static fields/variables can be hidden by subclasses.final only means that subclasses cannot hide (in case of static methods, and static or non-static fields) or override (in case of instance methods) that method.
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1069 :
Yes, that's what it says, (and static or non-static fields). Overrides applies only to instance methods.
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Wed Feb 17, 2021 6:55 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1069 :
Hi, with regard to the 4th option where the answer states "final only means that subclasses cannot hide (in case of static methods, and static or non-static fields)" - I have tried the following code and even though the data is "final" in the parent, I am able to hide the data in the subtype..?
Kind regards, Seán.
Kind regards, Seán.
Code: Select all
package oca.java_basics;
class Base{
final static int i=4; // constant
final int j=5; // constant
final void im(){} // cannot override (instance)
final static void sm(){}// cannot hide (static)
// Base(){i=0; j=0;} // compiler errors
Base(){}
}
class Sub extends Base{
static int i=6;
int j=7;
// void im(){} // compiler error if we try to override
// static void sm(){} // compiler error if we try to hide
}
public class Q2_1069 {
public static void main(String[] args) {
Base b = new Sub();
System.out.println(b.i);// 4
System.out.println(b.j);// 5
Sub s = new Sub();
System.out.println(s.i);// 6
System.out.println(s.j);// 7
}
}
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1069 :
You are right, Sean. That doesn't seem to be correct. Not sure why it was added. Fixed.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Wed Feb 17, 2021 6:55 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1069 :
No problem at all. Thanks for the prompt response. /Seán.
Who is online
Users browsing this forum: No registered users and 124 guests