By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 819 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

inner class constructors

P: n/a
hi, i am having a small problem with constructing an inner class.
i am using an inner class (and not a static nested class) because the
methods of the inner class need access to the enclosing object's members.

now, i have an enclosing class, which has a static factory method (loads
an instance of the enclosing class from a serialized file, and returns
it).
during the loading, some initialization takes place. one of the
initialization procedures is to create an instance of the inner class,
through that inner class' constructor.
and now the problem becomes somewhat evident... i have a static method
(the factory method) trying to create an instance of an inner class which
technicall doesn't exist until an instance of the enclosing class has been
created...

so the code looks akin to this:

class EnclosingClass
{
private transient InnerClass innerClassInstance;
private Object innerClassConstructorParameter;

private class InnerClass
{
InnerClass(Object o)
{ /* some code... */}
}

public static EnclosingClass loader(File f)
{
ObjectInputStream s = new ObjectInputStream(new
FileInputStream(f));
EnclosingClass theInstance =
(EnclosingClass)s.readObject();
theInstance.innerClassInstance = new
EnclosingClass.InnerClass(theInstance.innerClassCo nstructorParameter);
}
}

the last line of code is where the error is... and i understand the
error... kinda.

the inner class constructor (indeed the inner class itself) exists within
a particular enclosing class instance... so i cannot call it from a
typical enclosing class' static method. but in the case above, i do have
a valid instance of the enclosing class (theInstance).

so, the question is, how can i now create an instance of the inner class
that exists within the enclosing class instance (theInstance)?

my best guess was to do something like this:

theInstance.innerClassInstance = new
theInstance.InnerClass(theInstance.innerClassConst ructorParameter);

but that didn't work, because the compiler then looked for a namespace
called "theInstance".

is this just something that i cannot do directly?

p.s. i have found a work-around... create another non-static method in the
enclosing class that initializes (and creates) the inner class instance.
even so, i'd like to find the more elegant solution that doesn't require
this extra step. (and since this is such an easy work-around, i know my
understanding of the inner class/enclosing class relationship is fine...
it's just the physical question of how to code it properly...)

thanks again for all the help,

murat

--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Murat Tasan wrote:
p.s. i have found a work-around... create another non-static method in the
enclosing class that initializes (and creates) the inner class instance.
even so, i'd like to find the more elegant solution that doesn't require
this extra step. (and since this is such an easy work-around, i know my
understanding of the inner class/enclosing class relationship is fine...
it's just the physical question of how to code it properly...)


Murat,

As far as I know, that is the only solution.

You may know this already, but keep in mind that the ability of an inner
class to access the enclosing classes private data is really a trick of
the compiler, not the JVM. That means that the compiler creates methods
allowing the inner class to access the private data. You can see these
methods using javap from the SDK.

It may be possible to link to the constructor created by the compiler
for the inner class at compile time. (I suspect the compiler will
create a constructor accepting the enclosing class instance.) You could
probably determine the signature for the constructor using javap.
However, I would hardly consider that "more elegant" and it may break in
other compilers.

Ray

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.