File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Nested Class - Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested Class - Issue" Watch "Nested Class - Issue" New topic

Nested Class - Issue

Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200

The code bolded in the above pgm are not working. Whether the instance of 'a'(top-level class) dosen't have access to the methods in the Nested class.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Hi Mark,

There are two different problems here, but they're both variations on a theme.

methodbb() is an instance method of class; therefore you need an instance of this class to call the method. If it were a static method, you could call it as

but since it's an instance method, you need an instance, like in your variable "b" in main.

Now the other line: methodaa() is an instance method of aa, so you need an instance of aa to call it. Because bb is a static class, there's no instance of aa associated with it. If bb were not marked static, this would work -- although then your initialization of variable b wouldn't work as written, because when constructing a bb you'd need to supply an aa.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Nested Class - Issue
It's not a secret anymore!