> > I have a class that looks like this
>
[quoted text clipped - 30 lines]
>
> Patricia
Patricia-
I am not trying to maintain str as a static variable. My intention is
to initialize str in the constructor of ClassNesting. Something like
this:
ClassNesting(String something){
//initialize str to something
}
Jim-
I figured that since Nest3() was essentially a part of ClassNesting I
should be able to access str through an instance of ClassNesting. I
cannot instantiate Nest3 anywhere except as ClassNesting's member
data.
What I want to do is initialize str through the constructor of
ClassNesting to the passed parameter. Thanks for your responses. I
hope you can help me solve this! :-)
Let me paste a clearer picture here
class ClassNesting {
class Nest1{
class Nest2{
class Nest3{
String str;
String getStr(){
return str;
}
}
}
}
Nest1.Nest2.Nest3 n3; //I don't really want to do this.
ClassNesting(String s) {
}
static void main(String[] args) {
ClassNesting cn = new ClassNesting("Sumitra");
cn.n3.str = "Hello!"; //Able to access str here like so. Again, not
really what I want to do.
}
Owen Jacobson - 14 Jun 2007 22:55 GMT
> > > I have a class that looks like this
>
[quoted text clipped - 11 lines]
> > > }
> > > }
This is almost exactly equivalent to this:
class ClassNesting {
/* ... */
}
class Nest1 {
private final ClassNesting outer;
public Nest1 (ClassNesting outer) { this.outer = outer; }
}
class Nest2 {
private final Nest1 outer;
public Nest2 (Nest1 outer) { this.outer = outer; }
}
class Nest3 {
private final Nest2 outer;
public Nest3 (Nest2 outer) { this.outer = outer; }
String str;
public String getStr () { return str; }
}
In this code, would you expect creating a new ClassNesting instance to
also automatically create a new Nest3?
If so, why?
That above example is as close as possible to what the compiler
generates for you.
> > > I want to access str in the constructor ClassNesting() to set it. I
> > > don't want to change the access modifiers for anything. I guess I need
[quoted text clipped - 18 lines]
> I am not trying to maintain str as a static variable. My intention is
> to initialize str in the constructor of ClassNesting.
*what* str? Look at the example I made above.
Joshua Cranmer - 15 Jun 2007 00:08 GMT
> That above example is as close as possible to what the compiler
> generates for you.
No, using this$0 is closer. I've dumped the javap output and seen it for
myself.
Owen Jacobson - 15 Jun 2007 00:23 GMT
> > That above example is as close as possible to what the compiler
> > generates for you.
>
> No, using this$0 is closer. I've dumped the javap output and seen it for
> myself.
Right, but you can't use $ in identifiers to code you pass to javac --
they're reserved for generated code. The semantics are the same,
except for visibility.
Stefan Ram - 15 Jun 2007 00:28 GMT
>Right, but you can't use $ in identifiers to code you pass to javac
Then, why do I remember having done so?
Owen Jacobson - 15 Jun 2007 01:08 GMT
> >Right, but you can't use $ in identifiers to code you pass to javac
>
> Then, why do I remember having done so?
What do you know, I learn something new every day. Note that section
3.8 of the JLS does suggest using it is a bad idea.
"The $ character should be used only in mechanically generated source
code or, rarely, to access preexisting names on legacy systems."
Jim Garrison - 14 Jun 2007 23:22 GMT
> Jim-
> I figured that since Nest3() was essentially a part of ClassNesting I
> should be able to access str through an instance of ClassNesting. I
> cannot instantiate Nest3 anywhere except as ClassNesting's member
> data.
You have DECLARED Nest3 but not instantiated anything. Since Nest3
is declared inside other classes and is not static it is in a privileged
position with regards to accessing members of enclosing class instances
(I don't remember the rules and would have to research it). However,
just declaring it there does not create an instance of it when you
instantiate an enclosing class. There's a big difference between
declaration and instantiation.
Patricia Shanahan - 14 Jun 2007 23:42 GMT
....
> Let me paste a clearer picture here
>
[quoted text clipped - 12 lines]
>
> Nest1.Nest2.Nest3 n3; //I don't really want to do this.
Even that would not be enough to make the assignment to str work. You
would have had to initialize n3 to refer to some instance of Nest3.
> ClassNesting(String s) {
>
[quoted text clipped - 5 lines]
>
> }
The approach you are taking seems to be based on some misconceptions
about the behavior and use of member classes.
Why don't you explain the design problem you are trying to solve? That
way, there would be some chance someone could suggest a solution, rather
than just saying "That won't work.".
Patricia