Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regular inner class

 
pooja jain
greenhorn
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anand Ko
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
pooja jain
greenhorn
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


can a outer class access members of inner ?
thanks .
pooja .
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can a outer class access members of inner ?


Yes.

 
pooja jain
greenhorn
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike can you explain the above code .
thanks .
pooja .
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
Shouldn't the inner class be associated with an instance of outerclass?

Thanks .
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic