About Question com.enthuware.ets.scjp.v6.2.576 :

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 com.enthuware.ets.scjp.v6.2.576 :

Post by ETS User »

I dont understand the explanation for answer 2.
Why is answer 2 correct ?
Could somebody please give me a clear explanation ?
-------------------------------------------------------------------------------
Given the declaration

interface Worker { void perform_work(); }


which of the following methods/class are valid?

1.
Worker getWorker(int i)
{
return new Worker(){ public void perform_work() { System.out.println(i); } };
}

2.
Worker getWorker(final int i)
{
return new Worker() { public void perform_work() { System.out.println(i); } };
}

3.
Worker getWorker(int i)
{
int x = i;
class MyWorker implements Worker { public void perform_work() { System.out.println(x); } };
return new MyWorker();
}

4.
Worker getWorker(final int i)
{
class MyWorker implements Worker { public void perform_work() { System.out.println(i); } };
return new MyWorker();
}

5.
class TestClass
{
Worker getWorker(int i)
{
return new MyWorker( i);
}
public static class MyWorker implements Worker
{
int x;
MyWorker(int i) { x = i; }
public void perform_work( ) { System.out.println(x); }
}
}`

answer 1 is not correct
explanation: As method parameter i is not final it cannot be accessed from perform_work().
answer 2 is correct
explanation : As method paramter 'i' is final, it can be accessed from perform_work();
answer 3 is not correct
explanation : x is also not accessible from perform_work(). In fact, i and x are similar for all practical purposes.
answer 4 is correct
explanation : no errors
answer 5 is correct
explanation : MyWorker is a nested class (although static). So it can be instantiated like any other package level class eg. new OuterClass.MyWorker(10);

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

Re: About Question com.enthuware.ets.scjp.v6.2.576 :

Post by admin »

Can you please tell me what is it that you don't understand about option 2, so that I can explain?

-Paul.
If you like our products and services, please help us by posting your review here.

msajtos
Posts: 2
Joined: Mon Mar 18, 2013 8:36 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.576 :

Post by msajtos »

admin wrote:Can you please tell me what is it that you don't understand about option 2, so that I can explain?

-Paul.
- we have an interface named Worker
- we have a method named getWorker with Return Value Worker and parameter int i
- there is a difference in behaviour if the parameter is final or not final
- please explain me why

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

Re: About Question com.enthuware.ets.scjp.v6.2.576 :

Post by admin »

As per JLS Section 8.1.3, "Any local variable, formal parameter, or exception parameter used but not declared in an inner class must be declared final."

This is how they designed the language. There could be technical reasons for this, which you may understand after reading JLS or other design documents.
If you like our products and services, please help us by posting your review here.

msajtos
Posts: 2
Joined: Mon Mar 18, 2013 8:36 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.576 :

Post by msajtos »

admin wrote:As per JLS Section 8.1.3, "Any local variable, formal parameter, or exception parameter used but not declared in an inner class must be declared final."

This is how they designed the language. There could be technical reasons for this, which you may understand after reading JLS or other design documents.
Great Answer, thanks!

Post Reply

Who is online

Users browsing this forum: No registered users and 236 guests