File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner classes 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" Watch "inner classes" New topic

inner classes

swapnil paranjape
Ranch Hand

Joined: May 15, 2005
Posts: 126
class Red {
private static final int a = 10; // 1
protected static int b = 20; // 2
int c = 30; // 3
static class StaticNested {
int d = a; // 4
int e = b; // 5
int f = c; // 6
class NonStaticInner {
int g = a; // 7
int h = b; // 8
int i = c; // 9

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8
i. 9

i thought the ans should be 6 and 8
but the ans is only 6.

i thought ans is 8 because the nonstatic inner class can only have final staic but cant have just static
so b is static .so how soes it gets compiled.?

question 2...please explain this

class Red {
static class StaticNested {interface ABC {}} // 1
class NonStaticInner {interface DEF {}} // 2
interface GHI {} // 3

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. None of the above
ans is 2..please explain how?
[ May 26, 2006: Message edited by: swapnil paranjape ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
In the first code, you are not declaring a static variable in the inner class, you are just accessing the value of a static member of the outer class.

In the second code, an inner class cannot have a member interface.
Amisha Shah
Ranch Hand

Joined: Mar 03, 2006
Posts: 33
non-static inner class can access all the members of outer class , inlcuding static and non static. while static inner class can acces only static members of outer class , so comilation error at line 6.

whenever you put interface inside inner class , by defalut it becomes static. and non-static inner class can not have any static members except static final variables , so interface inside non-static inner class will generate error. while static inner class can have static members and so u can put interface as a member inside static inner class. and because of that compilation error at line 2.

Amisha Shah.<br />SCJP 1.4
I agree. Here's the link:
subject: inner classes
It's not a secret anymore!