• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Nested classes - Series of doubts

 
Ranch Hand
Posts: 363
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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."
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 363
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 363
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oooooooooooooh...
I missed the args part of main()!
Thanks for the help!
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 363
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please see my question in the above code.
Thanks in adv,
 
Faisal Ahmad
Ranch Hand
Posts: 363
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 363
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 363
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the use of an abstract inner class? How can we use it?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 363
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 363
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No replies yet?!!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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,...
 
Faisal Ahmad
Ranch Hand
Posts: 363
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 was born with webbed fish toes. This tiny ad is my only friend:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic