innerclasses
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
hi all,
apart from instantiating the innerclass
using this: outer.inner i = new outer().new inner(); outer o = new outer();
outer.inner i = o.new inner();can u also do this?
outer o = new outer();
inner i = o.new inner();?
it doesn't sit well with me can anyone help?
apart from instantiating the innerclass
using this: outer.inner i = new outer().new inner(); outer o = new outer();
outer.inner i = o.new inner();can u also do this?
outer o = new outer();
inner i = o.new inner();?
it doesn't sit well with me can anyone help?
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Everything there looks just fine. The key to remember is that you need an instance of an outer class before you can instantiate an inner class for it. When you run into a problem like this, you might just want to write a test script, like this:
As you can see, I simply commented out certain lines so that I could try different combinations to ensure that each combination would compile.
Corey
As you can see, I simply commented out certain lines so that I could try different combinations to ensure that each combination would compile.
Corey
posted 23 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
public class outer1{
private static class inner {}
public static void main(String[] args) {
outer1.inner i = new outer1().new inner();
//outer1 o = new outer1();
//outer1.inner i = o.new inner();
//outer1 o = new outer1();
//inner i = o.new inner();
}}
Can u explain why this gives runtime error. Pls note i have made the inner class static.
Also i have read that non-static inner class do NOT need the enclosing class instance to be instantiated. Can u explain this with example?
private static class inner {}
public static void main(String[] args) {
outer1.inner i = new outer1().new inner();
//outer1 o = new outer1();
//outer1.inner i = o.new inner();
//outer1 o = new outer1();
//inner i = o.new inner();
}}
Can u explain why this gives runtime error. Pls note i have made the inner class static.
Also i have read that non-static inner class do NOT need the enclosing class instance to be instantiated. Can u explain this with example?
Corey McGlone
Ranch Hand
Posts: 3271
posted 23 years ago
No, you haven't. By using the keyword static, you no longer have an inner class. Instead, you now have a top-level nested class. In order to instatiate it, you'd use something like this:
Notice that no instance of the outer class is created.
Be sure to look at the JLS, §8.1.2 Inner Classes and Enclosing Instances.
Corey
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Deepali Pate:
...Pls note i have made the inner class static...
No, you haven't. By using the keyword static, you no longer have an inner class. Instead, you now have a top-level nested class. In order to instatiate it, you'd use something like this:
Notice that no instance of the outer class is created.
Be sure to look at the JLS, §8.1.2 Inner Classes and Enclosing Instances.
Corey
| It's fun to be me, and still legal in 9 states! Wanna see my tiny ad? The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |









