?
Solved

Inner Class in Java

Posted on 2011-03-14
8
Medium Priority
?
666 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 1000 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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 1000 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

Independent Software Vendors: 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

770 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