Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Can't create inner class object outside the outer class help needed?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Can't create inner class object outside the outer class help needed?

    class Outer
    {
    final int outer=10;
    void display()
    {
    Inner in = new Inner();
    in.show();
    }
    class Inner{
    Inner()
    {
    System.out.println( "this value is displayed when an object is created "+outer);
    }
    void show()
    {
    System.out.println("This is inner class");
    }
    }
    }
    class InnerDemo{
    public static void main(String args[])
    {
    Outer out = new Outer();
    out.display();
    //Outer.Inner object = new Inner();
    }
    }
    Last edited by helloworld922; July 31st, 2012 at 12:21 PM. Reason: please use [code] tags


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    979
    Thanks
    7
    Thanked 206 Times in 182 Posts

    Default Re: Can't create inner class object outside the outer class help needed?

    It's hard to figure out either the code or the question here.

    Here's the code using "code" tags. Ie with [code] at the start of the code and [/code] at the end:

    class Outer
    {
       final int outer=10;
       void display()
       {
          Inner in = new Inner();
          in.show();
       }
       class Inner
       {
          Inner()
          {
             System.out.println( "this value is displayed when an object is created "+outer);
          }
          void show()
          {
             System.out.println("This is inner class");
          }
       }
    }
    class InnerDemo{
       public static void main(String args[])
       {
          Outer out = new Outer();
          out.display();
          //Outer.Inner object = new Inner();
       }
    }

    Now the question... Are you wondering why the last line (the one commented out) won't compile?

    The point about inner classes is that the class itself is associated with an instance of the outer class. You can't have (or create) an instance of an inner class without having an instance of the outer class already created to act as its context.

    If you try to create an instance of the inner class without specifying the context - by saying "new Inner()" - the compiler will grumble "What sort of Inner?! An Inner of what?!"

    The proper syntax to use to create an instance of an inner class is

    Outer.Inner object = out.new Inner();

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Dark knight (July 31st, 2012)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can't create inner class object outside the outer class help needed?

    yes i was wondering about the last line only thanx for the help....

Similar Threads

  1. Convert File Object to class<?> object
    By CEO in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 27th, 2012, 07:55 PM
  2. Replies: 3
    Last Post: June 17th, 2012, 07:22 PM
  3. Create image Jpeg from an object of Image class.
    By Ramandeep in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 31st, 2011, 11:34 PM
  4. In a class create an array list of elements of another class, help!
    By LadyBelka in forum Collections and Generics
    Replies: 3
    Last Post: May 4th, 2011, 06:00 PM
  5. How to create a new object of another class within a method
    By davie in forum Object Oriented Programming
    Replies: 1
    Last Post: April 16th, 2010, 06:53 PM