USE OF INNER CLASS

Dear All
PLease Expalin Why do we USE INNER CLASS What are the benefits of it.
Help is Greatly Appreciated.

Check out http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html

Similar Messages

  • What is the Use of Inner classes in Interface.

    Hi All,
    Most of us we know that We can define inner classes in the interface. Like
    public interface MyItf{
         Demo d = new Demo();     
         class Demo{
              Demo(){
              //some additional code here
    }Now I have following question in my mind:
    1. An Interface is pure abstract. Then why inner classes inside the interface?
    2. In what scenario, we can utilize these inner classes of interface?
    Plz Share your views on this...
    Thks for ur replies in advance.

    This we cando in defining Demo Class outside.That's no argument. You could write the programs in other languages, so why use Java? Just because you can use a top-level class instead, it's no argument against using an inner class. You also can make all attributes public... you don't o that either (I hope).
    Ok Also
    tell me how to pass an Object in inner class Demo. to
    the method of Interface.
    public abstract TheInterface.Demo doSomething(TheInterface.Demo d);
    Can u give some real time situation where this
    concept can be used.There are only very, very few. Just because it's possible, it doesn't mean it needs to be done or is done often.

  • What is the use of Inner class.?

    hello everyone.........
    I want toknow that what is the use of Inner class...?

    Judging from what's seen around here? Mainly obfuscation.

  • When  we going to use static inner class

    Hi
    when we r going use static inner class
    inner classes use for to create adaptorclasses that implement an interface.
    what about Static inner class
    if possible give some examples
    Thanks in adv

    static inner classes are used when the inner class does not require to access the encompassing class's variables/methods. By default non-static inner classes obtain a reference to the outer class instance through which they access the outer class variables and methods
    ram.

  • When Should I use the Inner Classes ?

    When Should I use the Inner Classes ?
    What is the advantage(s) and the disadvantage(s) ?

    When I use innerclasses?
    1) Allmost allways when I need simple owner child behavior.
    2) When I need a behaviour, that is quite small, and used only once, I make it anonymous inner class. For example specialised streams and threads.
    3) Enumerations

  • Can I use the inner class of one containing class by the other class

    Can I use the inner class of one containing class by the other class in the same package as the containing class
    eg I have a class BST which has inner class Enumerator. Can I use the inner class from the other class in the same pacckage as BST?

    Inner classes do not share the namespace of the package of the containing class. Also they are never visible to other classes in the same package.Believe what you want, then, if you're going to make up your own rules about how Java works. For people interested in how Java actually works, here is an example that shows why that assertion is false:
    package com.yawmark.jdc;
    public class Outer {
         public class Inner {
    }And...
    package com.yawmark.demo;
    import com.yawmark.jdc.*;
    public class Demo {
         public static void main(String[] args) {
              assert new Outer().new Inner() != null;
    }~

  • Is there a way of doing this without using an inner class

    hi, i have developed a tree applet but it takes ages to load on my machine. I was wondering if i could get rid of the inner class that is created when "valuechanged" is called (line 28). I am posting my whole code, i hope someone can help. Cheers Rups
    [email protected]
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.event.TreeSelectionEvent;
    import java.net.*;
    import java.applet.AppletContext;
    import java.util.Enumeration;
    public class SimpleTree extends JApplet  {
    JTree tree = new JTree();
      public void init() {
       new SimpleTree();
      public SimpleTree() {
      //  WindowUtilities.setNativeLookAndFeel();
        Container content = getContentPane();
        Object[] hierarchy =
          { "Some Useful Web Links ",
            new Object[] { "Microsft","http://www.microsfot.com"},
            new Object[]{"Yahoo", "http://www.yahoo.com"}
        DefaultMutableTreeNode root = processHierarchy(hierarchy);
        final JTree tree = new JTree(root);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)
        tree.getLastSelectedPathComponent();
        String hoopla = tree.getLastSelectedPathComponent().toString();
        if (node == null) return;
        if (node.isLeaf()) {
           try {
               getAppletContext().showDocument(new URL("file://c:/webstuff/"+hoopla),
    "viewer1");
             }catch(Exception f) {
                System.out.println("" + f);           
        content.add(new JScrollPane(tree), BorderLayout.CENTER);
        setVisible(true);
      public DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
        DefaultMutableTreeNode node =
          new DefaultMutableTreeNode(hierarchy[0]);
        DefaultMutableTreeNode child;
        for(int i=1; i<hierarchy.length; i++) {
          Object nodeSpecifier = hierarchy;
    if (nodeSpecifier instanceof Object[]) // Ie node with children
    child = processHierarchy((Object[])nodeSpecifier);
    else
    child = new DefaultMutableTreeNode(nodeSpecifier); // Ie Leaf
    node.add(child);
    return(node);
    public static void main(String [] args) {
    JFrame f = new JFrame("") ;
    Container contentPane = f.getContentPane();
    contentPane.setLayout(new FlowLayout());
    SimpleTree ta = new SimpleTree() ;
    ta.init() ;
    f.addWindowListener(new ExitListener());
    f.setSize(250,500);
    contentPane.add(ta,BorderLayout.CENTER);
    f.setVisible(true) ;

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.event.TreeSelectionEvent;
    import java.net.*;
    import java.applet.AppletContext;
    import java.util.Enumeration;
    public class SimpleTree extends JApplet implements TreeSelectionListener  {
          JTree tree = new JTree();
          public void init() {
          new SimpleTree();
          public SimpleTree() {
    //  WindowUtilities.setNativeLookAndFeel();
          Container content = getContentPane();
          Object[] hierarchy =
          { "Some Useful Web Links ",
            new Object[] { "Microsft","http://www.microsfot.com"},
            new Object[]{"Yahoo", "http://www.yahoo.com"}
          DefaultMutableTreeNode root = processHierarchy(hierarchy);
          final JTree tree = new JTree(root);
          tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
          tree.addTreeSelectionListener( this );
            tree.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)
            tree.getLastSelectedPathComponent();
            String hoopla = tree.getLastSelectedPathComponent().toString();
            if (node == null) return;
            if (node.isLeaf()) {
            try {
            getAppletContext().showDocument(new URL("file://c:/webstuff/"+hoopla),
            "viewer1");
            }catch(Exception f) {
            System.out.println("" + f);           
          content.add(new JScrollPane(tree), BorderLayout.CENTER);
          setVisible(true);
          public void valueChanged(TreeSelectionEvent e) {
          DefaultMutableTreeNode node = (DefaultMutableTreeNode)
             tree.getLastSelectedPathComponent();
          String hoopla = tree.getLastSelectedPathComponent().toString();
          if (node == null) return;
          if (node.isLeaf()) {
             try {
                getAppletContext().showDocument(new URL("file://c:/webstuff/"+hoopla),
                                    "viewer1");
             }catch(Exception f) {
                System.out.println("" + f);           
          public DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
          DefaultMutableTreeNode node =
             new DefaultMutableTreeNode(hierarchy[0]);
          DefaultMutableTreeNode child;
          for(int i=1; i<hierarchy.length; i++) {
             Object nodeSpecifier = hierarchy[ i ];
             if (nodeSpecifier instanceof Object[])  // Ie node with children
                child = processHierarchy((Object[])nodeSpecifier);
             else
                child = new DefaultMutableTreeNode(nodeSpecifier); // Ie Leaf
             node.add(child);
          return(node);
          public static void main(String [] args) {
          JFrame f = new JFrame("") ;
          Container contentPane = f.getContentPane();
          contentPane.setLayout(new FlowLayout());
          SimpleTree ta = new SimpleTree() ;
          ta.init() ;
    //      f.addWindowListener(new ExitListener());
          f.setSize(250,500);
          contentPane.add(ta,BorderLayout.CENTER);
          f.setVisible(true) ;
    }

  • Can I use anonymous inner class to implement a interface?

    I have tried many times,but still can't find a method to do it.Could you give me some advice?

    you can do something similar to this :
    Interface_name pointer_name=(new Interface_name(){
    define all the functions that are declared in the interface.
    });

  • When to use Nested Class/Inner Classes ?

    I am not very clear, when to use nested/inner classes..
    The scenario is :-
    class ABC
    //ABC need to store multiple instance of class XYZ
    class XYZ
    int member1;
    int member2;
    One approach is
    class ABC
    class XYZ
    //vector of class XYZ instances is stored in class-ABC
    or another approach is Class XYZ can be in separate JAVA file.
    Query:-
    1) Is there any difference between nested or Inner class...or are they same?
    2) When should they be used....Is it good to use in above scenario.
    3) What are the disadvtanges/advantages of using the Nested
    class.

    Query:-
    1) Is there any difference between nested or Inner
    class...or are they same?I really don't get it. Yes there is a difference between having an inner class, and a class in a separate file, but a nested class is an inner class.
    2) When should they be used....Is it good to use in
    above scenario.To write an inner class is a design decision. Do other classes need to know about the class XYZ or not? Do the XYZ class need to know about the inner working of the ABC class? How complex is the XYZ class etc.
    >
    3) What are the disadvtanges/advantages of using the
    Nested
    class.See above.
    /Kaj

  • Why and how to use "inner class"

    When i am learning advanced java language features,
    i could not understand why and when to use the "inner class",
    who can give me some examples?
    Thanks!

    You would use an inner class when an object needs visibility of the outer class. This is akin to a C++ friend.
    An example of this is an iterator. An iterator over some collection is typically implemented as an inner class of the collection class. The API user asks for an Iterator (from the Collection) and gets one - in fact they receive an instance of an inner class, but doesn't care. The iterator needs to be inner, as the iterator needs to see the internal data structures of the outer (collection) class.
    This could also be done with an anonymous class, as mentioned by spenhoet above. However, in the case of a collection, the role of the iterator is clear - thus it deserves its own class. And often there is more than one place an iterator can be returned (e.g. see java.util.List, which has several methods that return Iterators/ListIterators), thus it must be put in its own class to allow reuse of the code by outer class.

  • How to use inner class in axis2 in java?

    Hi,
    Iam very new in axis2. My language is java. What iam trying to do is, i need to create a xml format for the clients to send data to server using axis2 service.
    My demo xml format is,
    <test1>
    <test2>
    <test3>
    <test4></test4>
    <test5></test5>
    </test3>
    <test3>
    <test4></test4>
    <test5></test5>
    </test3>
    </test2>
    </test1>
    Iam trying to use nested inner class to generate this xml. Below is my demo java class. But unfortunately, or may be lac of knowledge, iam unable to create sub trees.
    public class EchoService{
    public MyInnerClass retMyInnerClass(MyInnerClass test1) {
    return test1;
    public class MyInnerClass {
    private String test2;
    public MyInnerClass() { }
    public void setTest2(String test2) {
    this.test2 = test2;
    public String getTest2() {
    return this.test2;
    Please help me to generate the web service for client.
    Thanks in advance.

    847897 wrote:
    well, i apologies for that fault. But i need the answer. It is urgent.That's your problem, not ours. However, I don't see the connection between your inner class and producing XML subtrees. Perhaps if you explained the problem, rather than your solution...
    [url http://sscce.org/]This page is usually a good place to start; otherwise [url http://www.catb.org/~esr/faqs/smart-questions.html]this one.
    Winston

  • Using static interface inner class.. how?

    Can anyone tell me how I'm able to use an inner class that is a static interface?
    Specifically I'm getting DocumentEvents through a DocumentListener..
    Then within DocumentEvents there is an inner class which is a static interface called:
    DocumentEvent.ElementChange, I'm trying to use the methods contained within the interface ElementChange.
    Can anyone throw a helping hand as to how I go about this?
    Thanks :)

    public class A ... {
    public static interface B {
    }Just creates a public interface called A.B
    How are you supposed to use this with your DocumentEvent? I can't say

  • Use inner class as collection

    Is there a way to get toplink to use an inner class collection to house my 1:m relationship. The collection needs some attributes of the containing object.
    Can i override how collection is constructed within the java, because it seems pretty clear the workbench doesn't support this.
    thanks
    craig

    Seem like an odd thing to do. If it were a static inner class it would be possible, but an instance inner class is probably not.
    If you really need a reference to the source object from the collection object, it might be better to accomplish this through making the inner collection class an outer class, or at least static. You could then set a back reference to the source object in the source object's set method for the collection. You would need to configure the mapping to use method access, or if using value holders you could lazily set the collection back reference from your get method for the collection. You could also make special get/set methods for TopLink to use to access the collection value that convert the special collect to and from a normal collection.
    In general it might be better to put the logic specific to the owning class in the owning class instead of the collection.

  • Problem with Outer and Inner Classes....or better way?

    Right now I'm trying to create an Inner class.
    My .java file compiles ok, and I create my jar file too.
    But when I try to instantiate the Inner class, it fails:
    java.lang.NoClassDefFoundError: com/myco/vlXML/vlXML$vlDocument.
    Here's the class code:
    public class vlXML{
        private ArrayList myDocList=new ArrayList(); //holds documents
        public vlXML(){
        private class vlDocument{
            public vlDocument(){
            //stuff goes here
        public vlDocument vlDOC(){
            return new vlDocument();
        public void addDocument(){
            vlXML xxx=new vlXML();
            vlDocument myDoc=xxx.vlDOC();
            myDocList.add(myDoc);
        public int getNumDocs(){
            return myDocList.size();
    }Then, from a jsp page, I call:
    vlXML junk1=new vlXML();
    junk1.addDocument();...and get the error...
    Can someone help me figure out why it's failing?
    Thanks....

    You nailed it - thanks....(duh!)
    While I have your attention, if you don't mind, I have another question.
    I'm creating a Class (outer) that allows my users to write a specific XML file (according to my own schema).
    Within the XML file, they can have multiple instances of certain tags, like "Document". "Document"s can have multiple fields.
    Since I don't know how many "Documents" they may want, I was planning on using an Inner Class of "Document", and then simply letting them "add" as many as necessary (thus the original code posted here).
    Does that seem like an efficient (logical/reasonable) methodology,
    or is there there something better?
    Thanks Again...

  • Inner class vs. imported class

    Hi everyone,
    I have entitiy beans created for a client's web app I'd like to use in the
    web service using WebLogic Workshop 7.0. Say the classes are imported like
    this in the services:
    import com.hoike.clientname.ap.bean.Invoice
    import com.hoike.clientname.ap.bean.Vendor
    Instances of these classes are used in callback methods and some of the
    service methods.
    When I generate the CTRL file, it actually adds those imported classes as
    inner class of the service defined.
    The problem is that when I try to used these services from another service,
    I cannot use the imported classes (as Invoice or Vendor), but instead I have
    to use the inner class (InvoiceService.Invoice or VendorService.Vendor)
    Does WebLogic Workshop 7.0 only allow you to use inner classes? Is there a
    way to use custom classes as method parameters?
    Thanks in advance!
    Makoto

    how do you declare your inner class?
    Is it (public)
    public static class MyInnerClassor (private)
    private static class MyInnerClassor (package)
    static class MyInnerClassor (protected)
    protected static class MyInnerClassTry to change the way you declare the inner class. Use protected or package or public instead.

Maybe you are looking for

  • Issues while Installing SOA 11g in windows 8

    Hi Experts, I am trying to install SOA Suite to my laptop which have windows 8. Data base and RCU and Weblogic everything is completed successfully with out any issues. I have used oepe-wls-indigo-installer-11.1.1.8.0.201110211138-10.3.6-win32 for we

  • Nokia 5800 about CONTACTS List

    i wanna ask why nokia 5800 doesnt show the contact number of a name in CONTACTS.... i should double tap the name so i can see the Mobile Number.... is there any updates in version so i can see the number...

  • Handle long-running EDT tasks (f.i. TreeModel searching)

    Note: this is a cross-post from SO http://stackoverflow.com/questions/9378232/handle-long-running-edt-tasks-f-i-treemodel-searching copied below, input highly appreciated :-) Cheers Jeanette Trigger is a recently re-detected SwingX issue (https://jav

  • Trying to get iTunes to run after relocating the My Music's default path.

    Ok. I just recently partitioned-off a section of my EHD & put all my music inside it. Next I relocated "My Music"s default location to that particular partitioned section of the hard drive, so it now go's straight to where all my music is. Now I have

  • SCD Type 2 in ODI

    Hi How to achieve SCD Type 2 in ODI. What are the process to be followed. Thanks.