Static Inner Class
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 )
- 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 )
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Jonathan,
I tried your code on my m/c, it works fine, there is Runtime error.
I tried your code on my m/c, it works fine, there is Runtime error.
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
"JavaRanch, where the deer and the Certified play" - David O'Meara
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi,
I posted the same problem in the Marcus site and got variety of responses.
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
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Not a clue!
I use 1.2.1
I use 1.2.1
"JavaRanch, where the deer and the Certified play" - David O'Meara
| 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 |






