Page 1 of 1

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

Posted: Thu Mar 07, 2013 7:45 am
by The_Nick
Hi,
Regarding nested class, are they part of the OCAJP? I thought they were not included but instead they were part of the OCPJP exam.

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

Posted: Thu Mar 07, 2013 9:58 am
by admin
They are not explicitly mentioned but some students have reported getting a question on it. So better to know the basics than to be surprised in the exam.

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

Posted: Wed Mar 13, 2013 5:52 am
by Ambiorix
I don't understand why int i isn't accessible from perform_work(); if it's not declared as final.

What rule/logic is at play here?

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

Posted: Wed Mar 13, 2013 8:52 am
by admin
Ambiorix wrote:I don't understand why int i isn't accessible from perform_work(); if it's not declared as final.

What rule/logic is at play here?
The rule is that if you want to access a method parameter in an inner class defined in that method, the parameter must be final.

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

Posted: Sat Mar 15, 2014 12:01 pm
by fasty23
admin wrote:
Ambiorix wrote:I don't understand why int i isn't accessible from perform_work(); if it's not declared as final.

What rule/logic is at play here?
The rule is that if you want to access a method parameter in an inner class defined in that method, the parameter must be final.
Would you please help me to understand this?

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

Posted: Sat Mar 15, 2014 9:30 pm
by admin

Code: Select all

public class TestClass {
    public static void main(final String[] args) { 
        class Inner{
            void m(){
                System.out.println(args); //this will work ONLY if args is final
            }
        }
        
        new Inner().m();
    }
}


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

Posted: Sun Mar 16, 2014 2:03 pm
by fasty23
admin wrote:

Code: Select all

public class TestClass {
    public static void main(final String[] args) { 
        class Inner{
            void m(){
                System.out.println(args); //this will work ONLY if args is final
            }
        }
        
        new Inner().m();
    }
}


thank you.
What is the logic?

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

Posted: Sun Mar 16, 2014 9:09 pm
by admin
JLS hasn't elaborated on the rule. You may go through section 8.1.3 though: http://docs.oracle.com/javase/specs/jls ... ml#d5e9405