Results 1 to 2 of 2
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Why can't my Inner Class object use Outerclass method ?

    Java Code:
    package myPackage;
    
    public class DotThis 
    {
    	void f()
    	{
    		System.out.println("DotThis.f()");
    	}
    	
    	class Inner
    	{
    		public DotThis outer()
    		{
    			return DotThis.this;
    		}
    	}
    	
    	public Inner inner()
    	{
    		return new Inner();
    	}
    	
    	public static void main(String[] args)
    	{
    		DotThis dt = new DotThis();
    		DotThis.Inner dti = dt.inner();
    		dt.f();
    		dti.f();
    	}
    }
    dti.f() does not work. Why?

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why can't my Inner Class object use Outerclass method ?

    I will answer my own question:

    Inner Classes have access to methods and instance variables of the outerclass, not the Objects you have.

    So an Inner object can not call the f() method. But you can add a ff() method, that calls the f() method in the DotThis class, even if it was private.

    But you must know this as well:
    in your ff() method, you will have a secret Argument: DotThis.this , not a plain this. So you are still calling the f() method of the outer object.

Similar Threads

  1. Replies: 1
    Last Post: 11-29-2011, 01:30 PM
  2. Unable to access getter method in an object class I made
    By EnSlavingBlair in forum New To Java
    Replies: 6
    Last Post: 10-01-2011, 10:52 AM
  3. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  4. [SOLVED] Class-level vs Object-level method()
    By mfaizan24 in forum New To Java
    Replies: 7
    Last Post: 06-23-2009, 09:18 AM
  5. Object class's equals() method behavior????
    By skyineyes in forum New To Java
    Replies: 4
    Last Post: 07-19-2008, 11:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •