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 7 of 7

Thread: Inner Class Use

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Location
    INDIA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Inner Class Use

     
    class Outer
    {
       private int temp=9;
       class Inner
       {
          void getData()
          {
              System.out.println("Value of temp is"+temp);
           }
       }
       public static void main(String args[])
       {
          Outer o=new Outer();
          Outer.Inner i=o.new Inner();
          i.getData();
       }
    }
    [B][/B]


    In the code above inner class needs to access outer class variable to perform certain task.
    But why can't we just add a instance variable of Outer class in Inner class and keep both classes separate.Why do we need to nest it.What is the need of Inner Classes in java?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Inner Class Use

    The quick and simple answer is Encapsulation. This is an Object Oriented principle which essentially states you need to limit the access users have to your API. You should only be exposing what requires exposure to other classes and/or other packages.

    Balancing maintainability and readability comes into play alot with inner classes, as you don't want a massive class of inner classes. However, limiting the exposure of classes to outside packages means you risk breaking other peoples code when you change some functionality within your hierarchy different classes and methods.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,329
    My Mood
    Mellow
    Thanks
    253
    Thanked 781 Times in 766 Posts

    Default Re: Inner Class Use

    Or you could do as you've suggested, and I don't think there's anything wrong with it other than it may be more complicated than it needs to be:
    class Outer
    {
        private int temp=9;
        class Inner
        {
            int temp;
     
            public Inner( int temp )
            {
                this.temp = temp;
            }
     
            void getData()
            {
                System.out.println("Value of inner class temp is " + temp );
            }
        }
        public static void main(String args[])
        {
            Outer o=new Outer();
            Outer.Inner i=o.new Inner( o.temp );
            i.getData();
        }
    }

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Inner Class Use

    On the variable part of the question, the best resources is Shadowing. Hopefully this lets you understand the relation a little better.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Location
    INDIA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inner Class Use

    Hello,
    Your code is also using inner class.I wan't to know why can't we separate the two classes as independent and with the help of getter methods of Outer Inner class , Inner class can access it's variable.Why do we need to nest it?

    regards
    Swapneel

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Inner Class Use

    You do not need to nested it. It is an option. You can do anything you like.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,329
    My Mood
    Mellow
    Thanks
    253
    Thanked 781 Times in 766 Posts

    Default Re: Inner Class Use

    Who says you can't? Of course you can do exactly as you described. Unless you're in a class and the teacher tells you one way will get a good grade and another way will get a bad grade. Do the way that will get the good grade.

Similar Threads

  1. Java, calling another public class from within the main class giving problems.
    By RandomGaisha in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 26th, 2012, 02:30 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 09:03 PM
  4. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 04:21 AM
  5. Replies: 3
    Last Post: June 17th, 2012, 07:22 PM

Tags for this Thread