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.

Similar Messages

  • How to use a static inner class?

    what's the implication of identifier "static" before an inner class.Thank you.

    not sure what implication is, due to my poor vocabulary. But here's an uuh... "example" of a static inner class:
    public class Example {
         static class AnotherClass {
    }There you have class "Example" with the static inner class "AnotherClass." If you wanted to use "AnotherClass" you would do something like:
    public class Whatever {
         Example.AnotherClass ac = new Example.AnotherClass();
    }I don't think I'm mistaken there, but if I am someone please correct me.

  • 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;
    }~

  • Static inner class causes deployment  on OC4J 10.1.2 to fail

    Hi,
    this issue has already been raised on OC4J 9.0.4 with J2SDK 1.4.2 (see EJB fails to compile - static inner class problem
    Recap: When referencing static inner classes in an EJB, the deployment fails. During the generation of the wrapper classes, a signature <package>.<outer class>.<inner class> gets converted to <package>.<outer class>$<inner class>, which is syntactically wrong. While the Java 1.4.1 compiler kindly ignores this syntax error, its 1.4.2 counterpart complains - and fails.
    @Oracle: When will this bug be fixed for OC4J 10.1.2?
    Best regards,
    Holger

    Holger,
    Don't static inner classes contradict the EJB specification?
    In any case, in answer to Andy's question: If you wish to file this as a bug, you can do so via the MetaLink Web site.
    [In fact, [i]MetaLink is your only option for submitting bugs for Oracle products -- as far as I know.]
    Good Luck,
    Avi.

  • Which one is better static inner classes or inheritance ?

    Hi,
    Consider following scenario,
    Class A does some database related work and Class B,C,D has more specific tasks for specific databases. For now B,C,D has more static information like driver name etc.
    1. I can either make class B,C,D as static inner classes OR
    2. classes B,C,D can extend class A.
    Case 1. makes it more flexible, if in future, B,C,D needs more than static methods.
    Case 2. can avoid complexity and cost of instantiating differnt objects based on differnt scenarios.
    Which approache is better in both ?
    Thanks

    Yes, I have seen abstract factory pattern , rather I have implemeted it at one place and in case 1. using abstract factory pattern is the way to initialize all classes.But my question is if I make all subclasses as a static inner classes, will it be better or efficient approach as compare to abstract factory pattern.Because Abstract factory patter adds more complications in code in turn it provides more flexibility.

  • EJB fails to compile - static inner class problem

    I am using OC4J 9.0.4 and Sun JDK1.4.2 and EJBs fail to compile if they reference objects that contain static inner classes. However, they successfully compile under JDK1.4.1.
    For example, I have a class like below:
    public class ValueObject
    public static class Key
    private Key key;
    private String value;
    Any references to ValueObject.Key inside the EJB cause the EJB compiler to generate "ValueObject$Key" which is incorrect. JDK1.4.1 is more lenient than 1.4.2, and allows this error through.
    Is there a way around this problem other than reverting to JDK1.4.1?
    Regards,
    Andy

    The reason I don't want to move to JDK 1.4.1 is because of the memory leak in the StringBuffer.toString() method.
    Is there anywhere I can submit this as a bug?
    Regards,
    Andy

  • How to dynamically load static (inner) class

    I have an urgnent question, any comments would be appricated.
    I have a static inner class definition.
    Class myObject
    public static class innerObject
    I want to dynamically load the static innerObject class such as
    Class c = Class.forName ( "myObject.innerObject" );
    innerObject t = ( innerObject ) c.newInstance();
    I can't do this as it give me ClassNotFound Exception, Do I need to load myObject first? any comments?
    The other alternative is that to define my myObject as having a static variable referencing the static class.
    Class myObject
    staic innerObject m_inner = new innerObject();
    public static class innerObject
    Is there any option in java allowing me to load a static variable?
    Many thanks
    Jay

    I can't do this as it give me ClassNotFound Exception,Use "myObject$innerObject" instead of "myObject.innerObject".
    Is there any option in java allowing me to load a
    static variable?What do you mean by that?

  • A question about non-static inner class...

    hello everybody. i have a question about the non-static inner class. following is a block of codes:
    i can declare and have a handle of a non-static inner class, like this : Inner0.HaveValue hv = inn.getHandle( 100 );
    but why cannot i create an object of that non-static inner class by calling its constructor? like this : Inner0.HaveValue hv = Inner0.HaveValue( 100 );
    is it true that "you can never CREATE an object of a non-static inner class( an object of Inner0.HaveValue ) without an object of the outer class( an object of Inner0 )"??
    does the object "hv" in this program belong to the object of its outer class( that is : "inn" )? if "inn" is destroyed by the gc, can "hv" continue to exist?
    thanks a lot. I am a foreigner and my english is not very pure. I hope that i have expressed my idea clearly.
    // -------------- the codes -------------------
    import java.util.*;
    public class Inner0 {
    // definition of an inner class HaveValue...
    private class HaveValue {
    private int itsVal;
    public int getValue() {
    return itsVal;
    public HaveValue( int i ) {
    itsVal = i;
    // create an object of the inner class by calling this function ...
    public HaveValue getHandle( int i ) {
    return new HaveValue( i );
    public static void main( String[] args ) {
    Inner0 inn = new Inner0();
    Inner0.HaveValue hv = inn.getHandle( 100 );
    System.out.println( "i can create an inner class object." );
    System.out.println( "i can also get its value : " + hv.getValue() );
    return;
    // -------------- end of the codes --------------

    when you want to create an object of a non-static inner class, you have to have a reference of the enclosing class.
    You can create an instance of the inner class as:
    outer.inner oi = new outer().new inner();

  • Why go for a static inner class than a regular static class

    Hello,
    What are the reasons to go for a static inner class? What benefits are available with a static inner class when compared to a static class?

    When a class is an integral part of another class, it doesn't make sense to create a top level class for it.
    Also there is no "static class" only static inner class.

  • 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.

  • 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) ;
    }

  • 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

  • Static inner class

    class A{
    static class B{
    public void instanceMethod()
    System.out.println("instance method");
    class C{}How do I access the instanceMethod() of class B from class C ?

    But what does new A.B() mean.Static means that
    doesent run inside any instance of the class, rather it runs with class
    itself. So shouldnt it have been just A.new B()
    No, a static nested class is just a static member of the outer class.
    All other static members are accessible as Outer.innerMember and
    just so with that static nested class. Your proposal:A.new B()doesn't make sense because 'new' is not a static
    member of outer class A.
    kind regards,
    Jos

Maybe you are looking for