It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes regular inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "regular inner class" Watch "regular inner class" New topic
Author

regular inner class

pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
in an regular inner class , can outer class access the inner class members . i think not but don't know the reason .
please help . thanks .
pooja


:d
Anand Ko
Ranch Hand

Joined: Dec 03, 2003
Posts: 79
Can't make out exactly what are u looking for ?
But still then hope this helps -

public class t {

class x {
public int y=10;
void callx() {
System.out.println ("callx");
}
}

static class y {
static void cally() {
System.out.println ("cally");
}
}

public static void main (String args[]) {
t t1 = new t();
t1.t1();
}
void t1() {
x x1 = new x();
x1.callx();
System.out.println (x1.y);
t.y.cally();
}
}

This code tries to create the object of inner class in one of the members of the outer class and tries to access the members.

- Anand


Anand<br />SCJP 1.4, SCWCD 1.4, SCEA 5.0(1/3)
pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213


can a outer class access members of inner ?
thanks .
pooja .
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
can a outer class access members of inner ?


Yes.



Mike Gershman
SCJP 1.4, SCWCD in process
pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
Mike can you explain the above code .
thanks .
pooja .
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Normally, i can be accessed from anywhere in the package, but j can only be accessed within class Inner.

However, an outer class has special access to the members of an Inner class.

Since i and j are instance members, they only exist when an instance of Inner is created. "this" in Outer refers to an instance of outer. "this.r" or just plain "r" can be used to access an instance member of inner.
Puja S
Ranch Hand

Joined: Jan 06, 2005
Posts: 51
Mike,
Shouldn't the inner class be associated with an instance of outerclass?

Thanks .
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
yes, inner classes are normally associated with an outerclass instance.

I'm not quite certain (I'd have to try) what would happen if you have an outer class return an instance of an inner class from a method and that outer class instance goes out of scope.

But that's more of a garbage collection problem I think, which would be worth investigating


42
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
OK, did some testing.
Seems the inner class instance will continue to exist even if the outer class instance goes out of scope (I'd kinda expected that but it never hurts to make sure).



sadly gives an OutOfMemoryException before the outer class can be garbage collected, so I've not yet been able to determine whether the inner class instance can survive that as well

 
 
subject: regular inner class