This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's Mock Test on Nested classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan's Mock Test on Nested classes

Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143
Hi,
I cannot understand this from Dan's mock exams:
class A { // 1
//private abstract void m1(); // 2
private abstract class B {} // 3
private static class C {} //4
}
This compiles correctly.
But
abstract class A { // 1
//private abstract void m1(); // 2
abstract class B {} // 3
private static class C extends B {} // 4
}
this gives an error at line 4.
Enjoy.java:4: no enclosing instance of type Enjoy is in scope
private static class C extends B {} // 4
Can anyone please explain me this.
Thanks,
Gayatri.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Gayatri,
The class declarations in your post appear to be modified versions of declarations from my exam. The error message refers to type Enjoy, but it does not appear in your post. Is something missing?


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143
Yes, Dan I modified the class while trying to solve the question.
Actually I had renamed class A to Enjoy.java since I already had a A.java in my working directory. Here is the correct class which does not compile.
Sorry if I confused you.
abstract class Enjoy { // 1
//private abstract void m1(); // 2
abstract class B {} // 3
private static class C extends B {} // 4
}
Enjoy.java:4: no enclosing instance of type Enjoy is in scope
private static class C extends B {} // 4
Thanks,
Gayatri.
Rama Kumar PV
Greenhorn

Joined: Dec 26, 2003
Posts: 26
Class C is a nested class(since it is static) and it can access only the static members of the outer class. As class B is a non-static inner class, it cannot be extended by C. Hope this helps..
Cheers,
Rama
Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143
Thanks Rama.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dan's Mock Test on Nested classes