Solved

Inner Class in Java

Posted on 2011-03-14
8
653 Views
Last Modified: 2012-05-11
I compiled and run the code below, and the output is , "middle". Could you please explain why the code prints "middle" instead of "outer"?  I can understand why "inner" does not print because it comes after  - new A().m(); But "Outer" is in the beginning so it does not make sense why "Outer" does not print.

class A
{
	void m()
	{
		System.out.println("Outer");
	}
}

public class TestInners
{
	public static void main(String [] args)
	{
		new TestInners().go();
	}
	
	void go()
	{
		
		new A().m();
		class A
		{
			void m()
			{
				System.out.println("Inner");
			}
		}
	}
	
	class A
	{
		void m()
		{
			System.out.println("middle");
		}
	}
}

Open in new window


0
Comment
Question by:techbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 7

Expert Comment

by:rodness
ID: 35132770
Within the scope of the go() method, the local class TestInners.A has precedence over the global class A.  As for the class that would print "inner", you are right, it hasn't been defined yet.

This is, of course, why reusing class names, even for inner classes, is a cause for confusion.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 35132775
thats because the "this" reference is attached to it, it is as good as this.new A()
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 250 total points
ID: 35132808
this might help u understand this,

package test;

/**
 * TYPE DESCRIPTION HERE
 *
 */
class A
{
      void m()
      {
            System.out.println("Outer");
      }
}

public class TestInners
{
      test.A a = new test.A() ;
      public static void main(String [] args)
      {
            new TestInners().go();
      }
      
      void go()
      {
            a.m();
            System.out.println( A.class ) ;
            
            this.new A().m();
            class A
            {
                  void m()
                  {
                        System.out.println("Inner");
                  }
            }
            
            this.new A().m();
      }
      
      class A
      {
            void m()
            {
                  System.out.println("middle");
            }
      }
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:techbro
ID: 35133183
Thank your for your response.

One more question before closing.
I tried your code, and it helped me understand how local class has more precedence more global class. It makes sense why the method prints "middle" in line 3, but why "this.new A().m()" prints "middle" in line 12. Without "this", it is printing "inner".

The code is given below:

void go()
{
            this.new A().m();
            class A
            {
                  void m()
                  {
                        System.out.println("Inner");
                  }
            }
            
            this.new A().m();
}

Open in new window

0
 
LVL 7

Assisted Solution

by:rodness
rodness earned 250 total points
ID: 35133196
With "this" it's going to reference the TestInners.A class ("middle").

Without "this", it's going to see the closest scoped A class, which is the one that was defined within the go() method ("inner").
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 35133256
>>this.new A().m()" prints "middle" in line 12

this is same as test.new A(), simply overriding the default reference. if you simply say new A(), this will refer the inner class with in the go method. when you say this.new A(), you are overriding the default and telling the runtime to refer the class refered by TestInner( nothing but "this" )

>>Without "this", it is printing "inner".

see above
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 35133284
compare it with pointing same file name located in different dir.
0
 

Author Closing Comment

by:techbro
ID: 35133369
Thank you for your time!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 84
configure dependency in POM for new database 3 45
Chrome and Firefox Java 5 68
running on tomcat not jboss eap 7.0 3 31
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question