Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1069 :

Posted: Sun Feb 03, 2013 11:39 am
by ETS User
Answer requires for us to select 2 options but isn't option 1 right also?

Re: About Question enthuware.ocajp.i.v7.2.1069 :

Posted: Sun Feb 03, 2013 2:49 pm
by admin
Did you read the detailed explanation?

Re: About Question enthuware.ocajp.i.v7.2.1069 :

Posted: Sat Apr 20, 2013 10:43 am
by smearaDubha
admin wrote:Did you read the detailed explanation?
The explanation for Option 1 says :

"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

Re: About Question enthuware.ocajp.i.v7.2.1069 :

Posted: Sat Apr 20, 2013 12:46 pm
by admin
smearaDubha wrote:
admin wrote:Did you read the detailed explanation?
The explanation for Option 1 says :

"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
Please read all the explanation provided with the question. It also says,
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.

Re: About Question enthuware.ocajp.i.v7.2.1069 :

Posted: Sat Feb 18, 2017 2:53 am
by Deleted User 3513
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.
During the exam, should I always expect that the main method is public? and wrong if not? Please advice.

Re: About Question enthuware.ocajp.i.v7.2.1069 :

Posted: Sat Feb 18, 2017 3:58 am
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.1069 :

Posted: Fri Nov 01, 2019 6:36 pm
by DazedTurtle
In the book, section 12.3.3, it says the order is

Code: Select all

<access modifier> <static> <final or abstract> <return type> methodName(<parameter list>)
And it says that if you ever get confused, you can use a final version of the main method to figure it out:

Code: Select all

public static final void main(String[] args)
I specifically made an effort to create a mnemonic for this ("Prominent Superhero Fights Villainous Menace," if anyone cares).

However, this particular question lists

Code: Select all

final public static void main(String [ ] array)
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?

Re: About Question enthuware.ocajp.i.v7.2.1069 :

Posted: Fri Nov 01, 2019 9:03 pm
by admin
The book also says this before talking about the order:
The exam, however, does not try to trick you on the order
of access modifiers and final and abstract keywords.
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 book should make this clear to avoid confusion.

Re: About Question enthuware.ocajp.i.v7.2.1069 :

Posted: Tue Nov 26, 2019 1:02 am
by pollentem@gmail.com
The explanation for the option "final public static void main(String [ ] array)" says:
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.
This confuses me because I know and I've tried that final static or non-static fields/variables can be hidden by subclasses.

Re: About Question enthuware.ocajp.i.v7.2.1069 :

Posted: Tue Nov 26, 2019 1:55 am
by admin
Yes, that's what it says, (and static or non-static fields). Overrides applies only to instance methods.

Re: About Question enthuware.ocajp.i.v7.2.1069 :

Posted: Fri Sep 24, 2021 5:21 am
by Seán Kennedy
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.

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
    }
}

Re: About Question enthuware.ocajp.i.v7.2.1069 :

Posted: Fri Sep 24, 2021 6:14 am
by admin
You are right, Sean. That doesn't seem to be correct. Not sure why it was added. Fixed.
thank you for your feedback!

Re: About Question enthuware.ocajp.i.v7.2.1069 :

Posted: Fri Sep 24, 2021 6:57 am
by Seán Kennedy
No problem at all. Thanks for the prompt response. /Seán.