Solved

Inner Class in Java

Posted on 2011-03-14
8
634 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
basic hardware to learn oop advanced design patterns 3 88
Securing Jmx Console and web console 2 66
Image decoding from Camera 3 73
more than one jdk and one jre 1 34
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

863 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

25 Experts available now in Live!

Get 1:1 Help Now