This week's giveaway is in the Spring forum.
We're giving away four copies of liveProject: Protecting User Data with Spring Security and OAuth2 and have Laurentiu Spilca on-line!
See this thread for details.
Win a copy of liveProject: Protecting User Data with Spring Security and OAuth2 this week in the Spring forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Dan's Mock Test on Nested classes

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Gayatri Ganesh
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rama.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic