Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Icky Nesting

Thread view: 
Sum - 13 Jun 2007 22:24 GMT
I have a class that looks like this

class ClassNesting {

    class Nest1{
        class Nest2{
            class Nest3{
                String str;

                String getStr(){
                    return str;
                }
            }
        }
    }

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
to figure the theory out for this. Any help? How can I do that?

Thanks! :-)
Jim Garrison - 13 Jun 2007 22:51 GMT
> I have a class that looks like this
>
[quoted text clipped - 15 lines]
> don't want to change the access modifiers for anything. I guess I need
> to figure the theory out for this. Any help? How can I do that?

str doesn't exist until an instance of Nest3 is created.
This can only happen at a 'new' expression for Nest3.
Instantiating ClassNesting, Nest1, or Nest2 does NOT
create an instance of Nest3.

Maybe if you explained what you're trying to accomplish
we could help.
Patricia Shanahan - 13 Jun 2007 22:57 GMT
> I have a class that looks like this
>
[quoted text clipped - 17 lines]
>
> Thanks! :-)

Since str is not static, and is declared inside Nest3, each Nest3
instance has its own str.

Talking about "set it" suggests you think there is exactly one str
field. Why? Does something ensure that there is exactly one Nest3
instance, not zero or forty-two?

If all Nest3 instances should use the same value for str, you could make
it a ClassNesting field. The ClassNesting constructor would set it, and
each Nest3 would have access to it.

Patricia
Sum - 14 Jun 2007 21:51 GMT
> > 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.