aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Nested classes - Series of doubts 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 "Nested classes - Series of doubts" Watch "Nested classes - Series of doubts" New topic
Author

Nested classes - Series of doubts

Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

How many classes can be nested within each other?
Can a static nested class contain an inner class?
Can an inner class embed a static class in it?
Help appreciated!
PS: The terminology I used:
I call the whole concept as nested classes. And the one that's marked as static is called static nested class. Rest - inner classes; which are again of 3 types viz. regualr inner class, method-local inner class and anonymous inner class.
[ March 07, 2007: Message edited by: Faisal Pasha ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Faisal Pasha:
...Can a static nested class contain an inner class?
Can an inner class embed a static class in it?
Help appreciated! ...

Testing this is fairly simple (especially for someone hoping to be certified as a programmer). For example...

...results in a compilation error: "modifier static not allowed here."


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
John Lincoln
Ranch Hand

Joined: Feb 11, 2003
Posts: 192
Hi,

There are can be any number of nested class. Static class can have an inner class. Following code compiled.

Marc answered static modifer cannot be used in the inner class
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Marc was right.
An inner class can never have any kind of static code in it, be it a method, a variable or a method. Whereas, a static nested class can have an inner class in it. However, refer this code:

Ignore any typos in the above code. If we are allowed to call a class's static method from another class, why can't we call it's main() which is also static?
Help is appreciated.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Faisal Pasha:
...If we are allowed to call a class's static method from another class, why can't we call it's main() which is also static? ...

You can definitely call another class's main method (using a String[])...

[ March 06, 2007: Message edited by: marc weber ]
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Oooooooooooooh...
I missed the args part of main()!
Thanks for the help!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Faisal Pasha:
...I missed the args part of main()! ...

You don't even need to create a String array, because you can call main with null...

Class2.main(null);

However, this could be ambiguous if you overload main to take other argument types. But the point is that main behaves exactly the same as any other method -- the difference is that it can also be used as an entry point.
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355


Please see my question in the above code.
Thanks in adv,
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Got it!
System.out.println(Outer.this.s);

Wow! Well, if my questions are too premature please don't laugh. Its that Im connected to internet always, I immediately post here what I feel..no waste of time..hehe. Well, don't you guys see the advantage? Im providing you the working code!
Thanks again!
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

How can we see an Inner class' instance variable from Outer class's instance method? Please see the following partial code listing..

Full code is already posted in this thread.
Help appreciated..
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

What is the use of an abstract inner class? How can we use it?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Faisal Pasha:
How can we see an Inner class' instance variable from Outer class's instance method? Please see the following partial code listing...

Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Thanks a lot marc!
Well, I have another doubt..
Every nested class has got some purpose. I didn't understand why method-local inner classes are made! Could you please help me understand?
Thanks!
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

No replies yet?!!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Faisal Pasha:
No replies yet?!!


Well I was sleeping at that time you posted. In fact I'm just going to roll out of bed and go to work. Sorry, absolutely no time for anything, but grabbing something to drink and a sandwich. Gotta go,...


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Originally posted by Barry Gaunt:


Well I was sleeping at that time you posted. In fact I'm just going to roll out of bed and go to work. Sorry, absolutely no time for anything, but grabbing something to drink and a sandwich. Gotta go,...

That's ok! Hope, you reply when you get some time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested classes - Series of doubts