Inner Classes Flashcards

1
Q

How to access fileds from a inner class

A

To Access fields from an inner class: this.field
To access fields from the outer class: ClassName.this.field:
EG:
public class AccessTest{
String a = “x”;
static char b = ‘x’;
String c = “x”;
class Inner{
String a = “y”;
String get(){
String c = “temp”;
// Line 1
return c;
}
}

AccessTest() {
System.out.println( new Inner().get() );
}

public static void main(String args[]) { new AccessTest(); }
}
Rezult:
AccessTest.this.a–> prints x
this.a–> prints y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly