Solved

Inner Class in Java

Posted on 2011-03-14
8
625 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
factorial example challenge 10 62
firstChar challenge 13 85
unix in java example 9 43
thymeleaf natural templating vs JSP 2 27
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now