Accessing the right method in Java

A inner class can call methods of an outer class.


public class Dummy1
{
	private class Dummy2 
	{	
		public Dummy2() 
		{
			method();
		}
	}
	
	private void method() { }
}

So far so good but if the inner class has the same method this will be called instead. To avoid this ambiguity one can call the method in a more specific way. To be able to call the method of the outer class the prefix Dummy1.this can be added.


public class Dummy1
{
	private class Dummy2 
	{	
		public Dummy2() 
		{
			Dummy1.this.method();
		}
		
		private void method() { }
	}
	
	private void method() { }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>