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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post by ETS User »

Answer requires for us to select 2 options but isn't option 1 right also?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Did you read the detailed explanation?
If you like our products and services, please help us by posting your review here.

smearaDubha
Posts: 6
Joined: Wed Apr 17, 2013 6:35 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Deleted User 3513

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

DazedTurtle
Posts: 26
Joined: Wed Oct 02, 2019 1:42 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

pollentem@gmail.com
Posts: 1
Joined: Sat Jun 15, 2019 12:47 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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.

Seán Kennedy
Posts: 12
Joined: Wed Feb 17, 2021 6:55 am
Contact:

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

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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!
If you like our products and services, please help us by posting your review here.

Seán Kennedy
Posts: 12
Joined: Wed Feb 17, 2021 6:55 am
Contact:

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

Post by Seán Kennedy »

No problem at all. Thanks for the prompt response. /Seán.

Post Reply

Who is online

Users browsing this forum: No registered users and 39 guests