Reputation: 7170
i've this code
public class MasterClass {
private String myVariable;
private ValuesClass objectA;
class ValuesClass {
public void method() {
myVariable = 1; // I can't access myVariable
}
}
}
How to access myVariable from inside ValuesClass ?
Upvotes: 2
Views: 246
Reputation: 52994
What you are doing is exactly correct. Except that myVariable
is a String
, and you are trying to assign an int
to it.
Now, if your inner class ALSO had a variable called myVariable
, you would need some special syntax to access the one from the outer class:
MasterClass.this.myVariable = ...
Edit by Martijn: This is called a Qualified This.
Upvotes: 10
Reputation: 8125
You can access it directly with myVariable
, or if you have conflicting variable names,
public class MasterClass {
private String myVariable;
private ValuesClass objectA;
class ValuesClass {
private String myVariable
public void method() {
MasterClass.this.myVariable = "Hello World!";
}
}
}
Upvotes: 1