Results 1 to 2 of 2

Thread: Inner class

  1. #1
    Swapneel is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Inner class

    Java Code:
    public class City
    {
         class Manhattan 
         {
             void doStuff() throws Exception { System.out.print("x "); }
         }
         class TimesSquare extends Manhattan 
         {
             void doStuff() throws Exception { }
         }
         public static void main(String[] args) throws Exception 
         {
             new City().go();
             new City().new TimesSquare();  // line 14
         }
         void go() throws Exception 
         {
             new TimesSquare().doStuff();  //line 18
         }
    }
    //On line 14 we need a reference of City to instantiate TimesSquare
    but why on line 18 why don't we need a reference of City to instantiate TimesSquare????

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Inner class

    Because "go" is an instance method, it can only be called on a valid instantiated instance of City, so in that method you ALREADY HAVE an instance of City.

Similar Threads

  1. Replies: 1
    Last Post: 12-13-2012, 04:06 AM
  2. Replies: 1
    Last Post: 11-29-2012, 10:46 AM
  3. Replies: 0
    Last Post: 12-07-2011, 12:55 PM
  4. Replies: 0
    Last Post: 03-27-2011, 06:49 AM
  5. Replies: 5
    Last Post: 01-26-2011, 07:38 PM

Tags for this Thread

Posting Permissions

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