0

I may use incorrect terms, sorry in advance. I need to access a property from instance of another class that is located in the the instance of outer class. There will be two instances of class Outer and I need to store and process property "desiredProperty" for each of them individually. Note: All classes are different. Inner1 and Inner2 are not the same classes! Here is a simple example.

File 1:

public class Outer{

public Inner1 inner1 = new Inner1();
public Inner2 inner2 = new Inner2();

}

File 2:

public class Inner1 {

int desiredProperty=1;

}

File 3:

public class Inner2{

public int getDesiredProperty(){

//How can I here access the property DesiredProperty from Inner1?

}

}
1
  • 1
    You need to have a reference to an instance of Inner1. Are the classes related in any way (or just called Outer and Inner)? Commented Jan 23, 2013 at 7:09

2 Answers 2

2

The Inner2 class need to have an instance property for Inner1

public class Inner2{

private Inner1 inner1;

public Inner2(Inner1 inner1){
   this.inner1 = inner1;
}

public int getDesiredProperty(){
    return inner1.getDesiredProperty();    
}

}
Sign up to request clarification or add additional context in comments.

Comments

0

first make a setter getter function in the Inner1 class, so you can get/set the value on Inner1

public class Inner1 {

int desiredProperty=1;
public int getDesiredProperty()
{
    return this.desiredProperty;
}

public void setDesiredProperty(int val)
{
    this.desiredProperty = val;
}

}

and in Inner2 class

public class Inner2{

public int getDesiredProperty(){

//How can I here access the property DesiredProperty from Inner1?
Inner1 inner1 = new Inner1();
return inner1.getDesiredProperty(); //value from Inner1
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.