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

Static Inner Class

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Static inner class like this...
class Outer1{
static class Inner{
public void aMethod(){
System.out.println("Hello Inner World");
}
}
public static void main(String[] arg){
// insert code here
in.aMethod();
}
}

a)
Outer1 o = new Outer1();
Inner in = o.new Inner();
b)
Outer1.Inner in = new Outer1().new Inner();
If I insert the option (a) in main method ,static inner class
method aMethod is called.
If I insert the option (b) in main method , Runtime error appears.(VerifyError is thrown.)
Couldn't anybody explain me why its so...as both are valid ways to create instances...

Jeban.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look, there are two type of inner classes viz., static and non-static
- to access members of static inner class use the first choice
- to access members of non-static inner class use the second choice (OR REMOVE THE static KEYWORD )
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jonathan,
I tried your code on my m/c, it works fine, there is Runtime error.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that this is caused because as a static inner class it applies to all objects of this outer class.
In case a) you created o.in and that is invoked even if you create Outer1 x = new Outer1. it is still o.in that prints the response for x.
In case b) you created ?.in. There is no name given to the outer class, so no way to reference it from outside the o ojbect (like from x). For this reason this syntax should not be used for a static class.
Hope this helps.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I posted the same problem in the Marcus site and got variety of responses.

  • Those Who used jdk 1.3 ,didnt got any problem with both the cases.No error, runtime or compile time; prints "Hello Inner World" as expected for them.
  • I used jdk 1.2
    For me ,I got problem with 2nd case case.
  • For some, even both case didn't work .

  • So I feel this problems varies depending upon the compiler one is using.
    The general feedback I got is this code just confusses the compiler and so should desisted from coding this way.
    So the correct way to code is :
    Outer1.Inner in = new Outer1.Inner();
    is correct both in or out of the scope of Outer1 class definition.
    or
    Inner in = new Inner();
    is correct only in the scope of Outer1 class definition.
    Jeban.

    Prasad ,
    R u using jdk 1.3?
    Cindy,
    Ur explanation is very reasonable.
    But why does it varies depending upon compiler one using?
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not a clue!
I use 1.2.1
 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic