[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Inner Class in Java

Posted on 2011-03-14
8
Medium Priority
?
673 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
Industry Leaders: 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 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

650 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