• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sun Guoqio's Mock 2 (anonymous class)

 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Please take a look at the following question (q.no 39)
link is http://www.geocities.com/sun_guoqiao/scjp/mockexam2answer.html

answer given is A, C, D
What about option E? The variable i is not defined inside of the method anywhere? Do we have to guess it may be an instance or static variable?
Thanks,
Vanitha.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Vanitha, please pay attention to the question:
<code>
Which of the following will NEVER compile correctly in any situation?
</code>
I add E into the question since I read some notes from others and mentioned there once appeared such kind of questions that need us to guess more than just the code. But does not happen very often.
E will compile if the variable i is defined in the supertype TestInterface. But A C D will definitely not compile since they got errors.
Hope it helps. If I am wrong, kindly let me know.

------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹² at my homepage.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Guoqiao Sun.
Yes, I read the question more than once, and I am not convinced that we have to guess that i might be defined in the supertype TestInterface.
Can anyone tell me do we have to guess at this type of situations?
Vanitha.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guoqiao,
I saw your explanation for the above question,
"Inner class can only access final variables of the enclosing method.",yes you are correct.
But I could not figure out that this could be an inner class, do we have to assume this as an inner class?. Can you please explain me what the code below is trying to do because i may not be very good at inner classes.
public TestInterface getInterface2( final int i )
{
return new TestInterface ( )
{
{
System.out.println(i);
}
};
}

Thanks,
Priyha
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, priyha, I would like to give the following example code as to your question:

As you can see, normally when you create your own thread, you need to explicitly write another class, either by extending Thread or implementing Runnable. While in our case, we create the class anonymously. By this way, we make the code more clean.
By the way, anynymous classes are mostly used in event delegation model, where we add event listener on the fly without subclassing all those event listener explicitly.
Hope it helps,

------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹² at my homepage.
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vanitha ,
hmm... i almost agree wid u
Assumptions required about option E) , like variable i may be from outer context , or i may be TestInterface's constant etc are bit too much .
Tht want u to play xtra-smart , n assuming too-much has its pitfalls , may even lead to incorrect situations.
But , in question it is mentioned to choose 3 correct options , and tht reduce any ambuguity in mind to almost zero level.
Well , but i am sure , we'll get only very-well-worked out questions in final exam , wid no ambiguity whtsoever , and above all , we 'll be told how many correct options to choose ,like in dis case also , which can help taking such decisions on da fly , if needed .

------------------
Gagan (/^_^\)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic