Erik Hosszú
Erik Hosszú

Reputation: 65

How to accesss hidden field in inner class (java)

I have the following problem.

class Master
{
   private String name;
   class Inner
   {
     private String name;
     private void printNames()
     {
       System.out.println("Master.name"+"Inner.name");
     }
   }
}

How can I access both name fields in the inner class without changing the name?

Upvotes: 1

Views: 212

Answers (1)

ernest_k
ernest_k

Reputation: 45339

You can use this:

System.out.println("Master.name: " + Master.this.name + 
                   " Master.Inner.name: " + this.name);

Master.this.name references the instance field of the outer class, Master, from within the inner class.

Upvotes: 3

Related Questions