Lost Password?

  #1 (permalink)  
Old 05-05-2008, 07:34 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 26
Posts: 4,862
Last Blog:
Zend Studio for Eclips...
Rep Power: 20
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default 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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Don't hesitate to ask any questions that you have! Check out our ASCII Calculator!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply

Tags
classes, java, tutorial



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial: Starting Java Using Netbeans Jordan Java Tutorials 0 04-05-2008 02:45 PM
Java tutorial : my sql with java Arkie Java Tutorials 2 04-05-2008 12:51 PM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 03:05 PM


All times are GMT -5. The time now is 07:34 PM.

Contest Stats

Xav ........ 164.00000
dargueta ........ 128.00000
John ........ 127.00000
gaylo565 ........ 18.00000
XaNaX ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

Ads