• Post Reply Bookmark Topic Watch Topic
  • New Topic

Access to private elements of the outer class  RSS feed

 
Robertovich Wartins
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
i have kind of tricky question.

Inner class has access to the private elements of its outer class - this i understand (i made example).
Question:
is it possible to access private elements of the outer class from inner class?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you mean to say something else? It sounds to me like your question is exactly the same as the statement you open with.

Let me perhaps answer the question without understanding it: by definition, all the code inside "class { ... }" can access all variables defined inside that class. This means that all code in an inner class can access the variables of the outer class, and all code in an outer class can access the variables in its inner classes. Does that help?
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course. I don't see the difference between the two sentences, except that one is a passive version of the other.
 
Robertovich Wartins
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry the question is -
is it possible to access private elements of the inner class from the outer class?
 
Robertovich Wartins
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
obviously it's impossible. the only way is this:
public class Outer {
static private class Inner {
private boolean someProperty = true;
}

void changeSomeProperty() {
//IMPOSSIBLE because someProperty and inner class isn't static
//someProperty = false;

//only way
Inner innerObj = new Inner();
innerObj.someProperty = false;
}

public static void main(String[] args) {
Outer obj = new Outer();
obj.changeSomeProperty();
}
}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Robertovich Wartins:
obviously it's impossible. the only way is this:
public class Outer {
static private class Inner {
private boolean someProperty = true;
}

void changeSomeProperty() {
//IMPOSSIBLE because someProperty and inner class isn't static
//someProperty = false;

//only way
Inner innerObj = new Inner();
innerObj.someProperty = false;
}

public static void main(String[] args) {
Outer obj = new Outer();
obj.changeSomeProperty();
}
}


But you are accessing the private element of the inner class from the outer class the only way you can. Even though it's an inner class, its instance variables still don't have any meaning except as part of an instance of that inner class.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Robertovich Wartins:
obviously it's impossible. the only way is this:


Aren't you contradicting yourself? First you say it's impossible, then you show a way to do it...
 
Robertovich Wartins
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only way if i create an instance of this Inner class inside method
void changeSomeProperty() {
// IMPOSSIBLE because someProperty and inner class isn't static
// someProperty = false;

// only way
Inner innerObj = new Inner();
innerObj.someProperty = false;
}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be impossible to access even the public instance variables of any other class without creating an instance of that class.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!