+ Reply to Thread
Results 1 to 2 of 2

Thread: Tutorial: Java Inner Classes

  1. #1
    Jordan Guest

    Tutorial: Java Inner Classes

    Inner Classes


    These are classes inside classes. Now these classes may seem to violate the OO concept but that is left to your discretion.
    Creating them is as same as you create a normal class. The only catch is that here the class is treated somewhat like a class variable. You can have any access modifiers for it, which is not possible for a normal class. Normal classes are public or default.

    Here is how you declare it.


    Code:
    classs OuterClass{
         class InnerClass{
         }
    }
    Here is the similarity between an a Class Variable and an inner class. Note that you can put any access specifier unlike the other class which is limited to a public and default.



    Code:
     class OuterClass{
        private int d; 
          private class InnerClass{
         }
    }

    The Code

    Code:
    public class InnerClass1 {
    
    /*
    * Execution starts here
    */
    public static void main(String[] args) {
        TheOuterOne.TheInnerOne obj = new TheOuterOne().new TheInnerOne();
        obj.Display();
        }
    }
    class TheOuterOne
    {
        private int x =1;
        class TheInnerOne
        {
            public void Display()
            {
                System.out.println("The value of variable is "+x);
                new TheInnerTwo(); //Making a new instance of the inner class
            }
        }
    
        private class TheInnerTwo
        {
            public TheInnerTwo() {
            System.out.println("Inside a private inner class.");
        }
    }
    }

    Output:

    Code:
    The value of variable is 1
    Inside a private inner class.

  2. CODECALL Circuit advertisement

     
  3. #2
    javapride is offline Newbie
    Join Date
    Feb 2009
    Posts
    1
    Blog Entries
    1
    Rep Power
    0

    Re: Tutorial: Java Inner Classes

    tanx for ur wonderful post.nw i cleared my doubts regarding the inner class for longtime.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Advanced Java:Tutorial - Tic-Tac-Toe
    By John in forum Java Tutorials
    Replies: 47
    Last Post: 11-28-2011, 11:05 PM
  2. Replies: 13
    Last Post: 09-09-2011, 09:18 PM
  3. need basic java tutorial
    By kiddies in forum Java Help
    Replies: 10
    Last Post: 08-04-2009, 04:09 AM
  4. Java:Tutorial - A better looking GUI
    By John in forum Java Tutorials
    Replies: 6
    Last Post: 01-12-2009, 03:31 AM
  5. Java UML Tutorial
    By gszauer in forum Java Help
    Replies: 4
    Last Post: 01-30-2008, 08:55 AM

Tags for this Thread

Bookmarks

Posting Permissions

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