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?

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.

  • 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

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

  • How to dynamically load an Image into a TableView when its row/cell becomes visible?

    Hi,
    I am building an application that shows tables with large amounts of data containing columns that should display a thumbnail. However, this thumbnail is supposed to be loaded in the background lazily, when a row becomes visible because it is computationally too expensive to to this when the model data is loaded and typically not necessary to retrieve the thumbnail for all data that is in the table.
    I have done the exact same thing in the past in a Swing application by doing this:
    Whenever the model has changed or the vertical scrollbar has moved:
    - Render a placeholder image in the custom cell renderer for this JTable if no image is available in the model object representing the corresponding row
    - Compute the visible rows by using getVisibleRect and rowAtPoint methods in JTable
    - Start a background thread that retrieves the image for the given rows and sets the resulting BufferedImage in a custom Model Object that was used in the TableModel (if not already there because of an earlier run)
    - Fire a corresponding model change event in the EDT whenever an image has been retrieved in the background thread so the row is rendered again
    Btw. the field in the model class holding the BufferedImage was a weak reference in this case so the memory can be reclaimed as needed by the application.
    What is the best way to achieve this behaviour using a JFX TableView? I have so far failed to find anything in the API to retrieve the visible items/rows. Is there a completely different approach available/required that uses the Cell API? I fail to see it so far.
    Thanks in advance for any hints here.

    Here’s what I have tried so far:
    I have defined a property in my model object that contains a weak reference to the image that is expensive to load. I have modeled that reference as an inner class to the object so I have a reference to its enclosing object. That is necessary because my cell factory otherwise has no access to the enclosing model object, which it needs to trigger loading the image in the background.
    The remaining problems I have is, that I don’t have sufficient control over the loading process, i.e. I need to delay the loading process until scrolling has stopped and abort it as soon as the user starts scrolling again and the visible content changes. Imagine that loading an image for a table row (e.g. a thumbnail for a video) takes 200ms to load and a user quickly scrolls through a few hundred records and then stops. With my current set-up, the user has to wait for all loading processes that were triggered in the cell factories to finish until the thumbnails of the records they are looking at will appear (imagine an application like finder to be implemented like that, it would simply suck UX-wise). In my swing application a background thread that loads images for the visible records is triggered with a delay and stopped as soon as the visible content changes. This works well enough for a good user experience. I don’t see how I can do this based on the cell API. It is nice to have all this abstracted away but in this case I do not see how I can achieve the same user experience as in my swing application.
    I also tried registering a change listener to the TreeCell’s visible property to make that control the image loading but I don’t seem to get any change events at all when I do that.
    I must be missing something.

  • How to dynamically load images into Flash

    I have a movie clip on the stage that I want to dynamically
    load images into (that constantly change), how would I go about
    doing this? Thanks.

    Use the Loader class to load in images.
    Then use addChild to add the Loader class into your MovieClip
    on the stage.
    As far as &amp;quot;constantly change&amp;quot; what
    do you mean by that? You could use setInterval, ther enterFrame
    event, or any other means to trigger a new image to be loaded into
    the Loader instance.
    Finally, you can use the Tween class to create some nice
    effects for the images (fade it, blur in, photo blend, masks,
    etc)

  • How to dynamically load jar files - limiting scope to that thread

    Dynamically loading jar files has been discussed a lot. I have read a quite a few posts, articles, and demo code for doing just that. However, I have yet to find a solution to my problem. Most people modify their system class loader and are happy. I have done that and was happy for a time. Occasionally, you will see reference to an application server or tomcat or some other large project that have successfully been able to load and unload jar files, allow for dynamic deployment of code, etc. However, I have not been able to achieve similar success; And my problem is much less complicated.
    I have an application that executes a thread to send a given file/message to a standard JMS Server Queue. Depending on the parameters selected by the user, this thread may need to communicate with one of a number of JMS Servers, ie. JBoss, WebLogic, EAServer, Glassfish, etc. All of which can be done with the same code, but each needs to load their own flavor of JMS Client Jar files. In this instance, spawning a separate JVM for each communication would work from a classloader perspective. However, I need to keep it in the family and run under the same JVM, albeit each JMS Server Connection will be created and maintained in separate Threads.
    I am close, I am doing the following...
    1. Creating a new URLClassLoader in the run() method of each thread.
    2. Set this threads contextClassLoader to the new URLClassLoader.
    3. Load the javax.jms.JMSException class with the URLClassLoader.loadClass() method.
    4. Create an initialContext object within this thread.
    Note: I read that the initialContext and subsequent conext lookup calls would use the Thread�s
    contextClassLoader for finding/loading classes.
    5. Perform context.lookup calls for a connectionFactory and Queue name.
    6. Create JMS Connection, etc. Send Message.
    Most of this seems to work. However, I am still getting a NoClassDefFoundError exception for the javax.jms.JMSException class ( Note step #3 - tried to cure unsuccessfully).
    If I include one of the JMS Client jar files ( ie wljmsclient.jar for weblogic ) in the classpath then it works for all the different JMS Servers, but I do not have confidence that each of the providers implemented these classes that now resolve the same way. It may work for now, but, I believe I am just lucky.
    Can anyone shine some light on this for me and all the others who have wanted to dynamically load classes/jar files on a per Thread basis?

    Thanks to everyone - I got it working!
    First, BenSchulz' s dumpClassLoader() method helped me to visualize the classLoader hierarchy. I am still not completely sure I understand why my initial class was always found by the systemClassLoader, but knowning that - was the step I needed to find the solution.
    Second, kdgregory suggested that I use a "glue class". I thought that I already was using a "glue class" because I did not have any JMSClient specific classes exposed to the rest of the application. They were all handled by my QueueAdmin class. However...
    The real problem turned out to be that my two isolating classes (the parent "MessageSender", and the child "QueueAdmin") were contained within the same jar file that was included in the classpath. This meant that no matter what I did the classes were loaded by the systemClassLoader. Isolating them in classes was just the first step. I had to remove them from my jar file and create another jar file just for those JMSClient specific classes. Then this jar file was only included int custom classLoader that I created when I wanted to instantiate a JMSClient session.
    I had to create an interface in the primary jar file that could be loaded by the systemClassLoader to provide the stubs for the individual methods that I needed to call in the MessageSender/QueueAdmin Classes. These JMSClient specific classes had to implement the interface so as to provide a relationship between the systemClassLoader classes and the custom classLoader classes.
    Finally, when I loaded and instantiated the JMSClient specific classes with the custom classLoader I had to cast them to the interface class in order to make the method calls necessary to send the messages to the individual JMS Servers.
    psuedu code/concept ....
    Primary Jar File   -  Included in ClassPath                                                      
    Class<?> cls = ClassLoader.loadClass( "JMSClient.MessageSender" )
    JMSClientInterface jmsClient = (JMSClientInterface) cls.newInstance()                            
    jmsClient.sendMessage()                                                                      
    JMSClient Jar File  -  Loaded by Custom ClassLoader Only
    MessageSender impliments Primary.JMSClientInterface{
        sendMessage() {
            Class<?> cls=ClassLoader.loadClass( "JMSClient.QueueAdmin" )
            QueueAdmin queueAdmin=(QueueAdmin) cls.newInstance()
            queueAdmin.JMSClientSpecificMethod()
        }

  • Dynamic loading of a class at runtime with known inheritance

    Hi,
    I am trying to dynamically load a class during runtime where I know that the class implements a particular interface 'AInterface'. Also, this class may be linked to other classes in the same package as that class, with their implementations/extensions given in their particular definitions.
    The class is found by using a JFileChooser to select the class that implements 'AInterface', and loaded up.
    Because the name of the class can be practically anything, my current approach only works for certain classes under the package 'Foo' with classname 'Bar'. Some names have been changed to keep it abstract.
    private AInterface loadAInterface(URL url) throws Exception {
         URL[] urls = { url };
         // Create a new class loader with the directory
         URLClassLoader cl = new URLClassLoader(urls);
         // Load in the class
         Class<?> cls = cl.loadClass("Foo.Bar");
         return (AInterface) cls.newInstance();
    }As you can see, all that is being returned is the interface of the class so that the interface methods can be accessed. My problem is that I don't know what the class or package is called, I just know that the class implements AInterface. Also note that with this approach, the class itself isn't selected in the JFileChooser, rather the folder containing Foo/Bar.class is.

    ejp wrote:
    The class is found by using a JFileChooser to select the class that implements 'AInterface', and loaded up.
    Also note that with this approach, the class itself isn't selected in the JFileChooser, rather the folder containing Foo/Bar.class is.These two statements are mutually contradictory...My apologies, I worded that wrong. My current approach (the one given in the code) selects the root package folder. However, what I want to be able to do, is to simply select a single class file. The current code just makes some assumptions so that I can at least see results in the program.
    As you said, if the root of the package hierarchy is known, then this could be achieved. The problem is that the user either selects the package root or the AInterface class, but not both.
    Is there a way to get package details from a .class file to be used in the actual loading of the class?

  • How to Dynamically Load jvm.dll without setting PATH

    Please help, invoking CreateJavaVM via a function pointer is returning -3, but I'm not sure why.
    When my PATH is setup up to include the jvm.dll and I invoke JNI_CreateJavaVM() directly, it works fine. But I want it to work regardless of my PATH setting, so I am trying to use LoadLibrary and dynamically load the jvm.dll and then a function pointer to invoke CreateJavaVM().
    The LoadLibrary returns non-NULL result, the GetProcAccress() returns non-null, but when I invoke the pointer to the CreateJavaVM call, it returns -3. I know -3 is JNI_EVERSION error, but any ideas what is wrong with the code below, such that it would give me this error?
    Currently my code is as follows:
    typedef jint (JNICALL CreateJavaVM_t)(JavaVM **pvm, void **env, void *args);
    JavaVM *m_jvm;
    JNIEnv *m_env;
    SetDllDirectory(L"C:\\Program Files\\Java\\jdk1.6.0_20\\jre\\bin\\client");
    HINSTANCE hVM = LoadLibrary(L"jvm.dll");
    if ( hVM == NULL ) {
       // report error
    CreateJavaVM_t *pfnCreateJavaVM = (CreateJavaVM_t*)GetProcAddress(hVM, "JNI_CreateJavaVM");
    #ifdef JNI_VERSION_1_6
    char cpChars[1024] = {0};
    sprintf_s(cpChars, "-Djava.class.path=%s", classPath); // classpath is defined elsewhere
    JavaVMOption options[1];
    options[0].optionString = cpChars;
    JavaVMInitArgs vm_args;
    vm_args.version = JNI_VERSION_1_6;
    vm_args.options = options;
    vm_args.nOptions = 1;
    vm_args.ignoreUnrecognized = JNI_TRUE;
    pin_ptr<JNIEnv*> env = &m_env;
    pin_ptr<JavaVM*> jvm = &m_jvm;
    int result = pfnCreateJavaVM(jvm, (void**)env, &vm_args);
    // This is where the failure occurs. result is -3 for some reason here...why?For some reason, pfnCreateJavaVM is returning -3, but I'm not sure why? Any ideas what is wrong with this code, such that it would give me a -3?
    Thanks in advance,
    Bill

    wkoscho wrote:
    For some reason, pfnCreateJavaVM is returning -3, but I'm not sure why? Any ideas what is wrong with this code, such that it would give me a -3?As an experiment try using a different jvm.dll from a different location. From my box there are several different jvm.dll under my jdk install dir.

  • 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

  • How to dynamically load data from DB in an HTML control present in jsp

    Hi Friends,
    Can anyone help me with this problem:
    I am working on a portal application. My requirement is to dynamically load data in an HTML control present in my JSPs. The controls are combo-boxes, text-fields, list-boxes etc. . Also, the events to load the data are like On Form load, On selecting a value from the combo-box, on clicking on a text-field etc.
    If any one can help me with a code snippet, than that would be highly appreciable. If not then the approach to achieve this will also be helpful.
    Thanks and Regards,
    Gaive.

    Refer
    http://www.developer.com/db/article.php/3384201
    http://www.developer.com/db/article.php/10920_3399331_1

Maybe you are looking for