• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

inner classes

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
q-1:
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.

q-2:
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic