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

Similar Messages

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

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

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

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

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

  • Private inner class and static private inner

    Hi,
    I understand the concept and usage of inner classes in general.
    When should we go for a private inner class and when for a static private inner class? I tried searching but it wasn't of much help.
    Basically I need to design a caching solution in which I need to timestamp the data object. After timestamping, data will be stored in a HashMap or some other collection. I'm planning to use a wrapper class (which is inner and private) which holds the data object and timestamp. I can make the program work by using either normal inner class or static inner class, however would like to know which is better in such case. Also If I can get some general guidelines as to when to use a staic inner class and when to use a normal inner class, it would help me.
    Thanks in advance.

    user1995721 wrote:
    When should we go for a private inner class and when for a static private inner class?
    I can make the program work by using either normal inner class or static inner class, however would like to know which is better
    If I can get some general guidelines as to when to use a static inner class and when to use a normal inner class, it would help me.Making the inner class static is helpful in that it limits visibility.
    If the inner class needs to access non-static fields or methods from the containing class instance
    the inner class has to be non-static.

  • Static abstract inner classes

    Sorry to sound stupid, but why would a class be static? I've encountered an example of an abstract static inner class and I'm not sure why it's static. Static methods I understand -- they are methods of the class and not the instance. So then, can someone explain to me a static class?
    Thanks,
    Erik

    You have encountered a static nested class (not a static inner class - an inner class is a nested class that is not static).
    A static nested class is a nested class that does not require an instance of its enclosing class to exist in order that it can be instantiated. - It is mainly used for code organisation, and can be treated just like a top-level class.
    In practice, most nested classes (except perhaps event handlers), tend to be static.
    For an example of a static nested class from the core APIs, have a look at HasMap.Entry

  • Why inner classes cannot have static declarations ?

    Hi Friends,
    When i tried to make static declarations on a inner class which is non static, i am getting compilation error saying "inner classes cannot have static declarations". I want to know reason behind this implementation.
    Code which i have tried:
    public class TestOuter
    class TestInner{
    static int i =10;
    public static boolean validate(int a){
    if(a==0)
    System.out.println("Invalid data");
    return false;
    return true;
    public static void main(String a[]){
    boolean result = new TestOuter.TestInner.validate(0);
    System.out.println("Result="+result);
    Thanks,
    Shiju V.

    so I think if the
    outer class is not static , then Inner class can't be
    static as well. This is incorrect. An enclosed class can be indeed static while the outer is not, and vice versa.
    The difference between static/non static in regards to enclosed classes is that the static ones are 'top-level' and cannot access the members of the enclosing class.
    The effect of making an enclosed class static means there is only one instance of any variables, no matter how many instances of the outer class are created. In this situation how could the static inner class know which variables to access of its non static outer class. Of course the answer is that it could not know, and thus an static inner class cannot access instance variables of its enclosing class.
    Now, regarding non-static inner classes, and trying to give a valid answer to the original post:
    As with instance methods and variables, a non-static inner class is associated with an instance of its enclosing class and has direct access to that object's instance variables and methods.
    TestOuter outer = new TestOuter();
    TestOuter.TestInner inner = outer.new TestInner();Because an inner class is associated with an instance (inner class implicitly keeps a reference to the object of the enclosing class that created it), it cannot define any static members itself. Static members cannot access the this pointer.
    So, in an ordinary (non-static) inner class, the link to the outer class object is achieved with a special this reference. A static inner class does not have that special this reference, nor would a static method/variable of an ordinary (non-static) inner class.

  • Trying to serialize inner class and getting various errors.

    Hi, I'm working on a program that contains the inner class User. user implements serializable, and writes just fine. For some strange reason, though, after I read it, I keep getting NullPointerExceptions in my code.
    I also tried it using externalization. The User class is essentially built around a HashMap. I have the following methods:
    public void writeExternal(ObjectOutput out) throws IOException {
    out.writeObject(mainMap);
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    mainMap = (Map) in.readObject();
    String test = (String) in.readObject();
    Whenever inputstream.readObject() is called, though, I get an InvalidClassException; "no valid constructor." The sad part is that I have the method:
    public User () {
    main = new HashMap();
    Does anyone know what the problem is??? Thanks a lot!

    Does anyone know what the problem is???The inner class is not a static inner class. Therefore your constructor User, is actually User(OuterClass). Solution - make the class static OR use read and/or write resolution to reconstruct the relationship.

  • Are enums and inner classes allowed in taglib function signatures??

    Hi,
    I have a taglib with the following function definition:
    <function>
            <description>Determine if viewing a patient attribute is allowed</description>
            <name>isViewingPatientAttributeAllowed</name>
            <function-class>com.example.admin.authorization.UserAuthorizer</function-class>
            <function-signature>boolean isViewingPatientAttributeAllowed(com.example.bean.User, com.example.bean.Patient.PatientAttribute, com.example.bean.Patient)</function-signature>
            <example>
                ${ncvi:isViewingPatientAttributeAllowed(user, patientAttribute, patient)}
            </example>
    </function>Where com.example.bean.Patient.PatientAttribute is an enum (note package names have been changed to protect the innocent;-). When I try to compile my web app I get the following error:
    org.apache.jasper.JasperException: The class com.example.bean.Patient.PatientAttribute specified in the method signature in TLD for the function ncvi:isViewingPatientAttributeAllowed cannot be found. com.example.bean.Patient.PatientAttributeIf I change PatientAttribute to a static inner class of Patient, I still get the error. Are enums and inner classes allowed in function signatures?
    Thx.

    I think that you'll find it easier to define a non-inner abstract class RefBase, that exposes a removeFromQueue() method, then extend that for your Ref class. That way, the queue just deals with RefBase instances (or ? extends RefBase).
    I think that any other approach is going to get the compiler confused, because the compile-time Ref depends on the parameterization of its defining class.

  • Reference to enclosing instance in inner class constructor

    Is there any Java compiler which assigns reference to enclosing instance in constructor of inner clase before invoking super class constructor?
    class Outer {
    class Inner extends Global {
    public Inner(int x) {
    // I want (Outer.this != null) here
    super();
    class Global {
    public Global(int x) {

    class Outer {
    class Inner extends Global {
    public Inner(int x) {
    // I want (Outer.this != null) hereOuter.this is never null at this point. A non-static
    inner class always has an implicit reference to an
    instance of the enclosing class.Try this:
    class Outer {
    int m;
    class Inner extends Global {
    public Inner(int x) {
    super(x);
    protected void init(int x) {
    xxx = Outer.this.m + x; // Null pointer exception!!!
    class Global {
    int xxx;
    public Global(int x) {
    init(x);
    protected void init(int x) {
    xxx = x;

Maybe you are looking for