aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner classes question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner classes question." Watch "inner classes question." New topic
Author

inner classes question.

Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

Given the declaration

which of the following methods are valid?


Select the two correct answers.
(a)The method labeled (1).
(b)The method labeled (2).
(c)The method labeled (3).
(d)The method labeled (4).
(e)The method labeled (5).
---

correct answers are (d) and (e).
why (a), (b) and (c) are not correct



Ref: K.Mughal page#383


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
a and b are incorrect as you cant make an instance of an Interface and 3rd is wrong because in that the inner class cant access the variable i of the method....

let me see the 4th and 5th options now


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
yup and in the 4th... the variable i is final so the inner class can access it....so its right
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

correct
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
Raju Champaklal wrote:a and b are incorrect as you cant make an instance of an Interface and 3rd is wrong because in that the inner class cant access the variable i of the method....

let me see the 4th and 5th options now


1st & 2nd are wrong not because you cant create instance of interfaces, Raju please read K&B inner classes chapter. while making anonymous inner classwhich implements an interface the syntax is :
SomeInterface s = new SomeInterface(){};

1st is wrong because it is accesseing a non final variable 'i' in the method.
2nd is wrong because it misses () in the declaration(return new IntHolder {....) it should have been: return new IntHolder(){...


SCJP 1.6 96%
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
oh ya....Neha is right.....
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

actually i didn't read properly raju;s reply i just matched the ans keys!
but Neha's explanation is 100% right!

Answer explanation in book:
--
The methods labeled (1) and (3) will not compile, since the non-final parameter i
is not accessible from within the inner class. The syntax of the anonymous class in
the method labeled (2) is not correct, as the parameter list is missing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner classes question.
 
Similar Threads
question about inner class
static class
why would the code below not run?
Anonymous class...please help
An Exception Problem