Create another instance of a dynamically loaded class - Java - java

So I have created an instance of a class by searching through jar files with no problems, I have it set to create an instance using c.newInstance() (Is instance the right word to use here?)
Later on in the program I may want to create another instance of that class if a certain event occurs. How can I go about creating this without having to search through all of the Jar files until I find the right one and then creating it again? Is there a way to create it somehow if I still have a reference to the first one?
Please assume that I do not know the name of the classes that will be loaded until runtime and there will be multiple classes that will be loaded.
Thanks

Save a reference to the Class of your object
Class c = dynamicObject.getClass();
and
and you can create a new instance like this (assuming there is a parameterless constructor)
Object anotherDynamicObject = c.newInstance(); // you can cast accordingly
else, say there is a consturctor that takes int, you can do
Constructor constructor = c.getConstructor(int.class);
Object anotherDynamicObject = constructor.newInstance(1);

Assuming "x" is the object you created ...
x.getClass().newInstance();

Related

Is it possible for a constructor to call itself?

Is it possible for a constructor to call a class which is related to it? For example (in the code below) I am trying to create a new clock with ClockTimer and call the AnalogClock construct which has a ClockTimer parameter type. It doesn't seem to work because it gives me an error, but creating a new AnalogClock with the ClockTimer parameter in the ClockFrame class appears to work. What do I need to parse in AnalogClock (in the ClockApp) in order for it to run?
From the outside, it looks like the "analogClock" object needs the "timer" object to exist first, and the "timer" object needs the "analogClock" object to exist first.
In reality, thought, "timer" doesn't do anything with the "analogClock" ("clockFrame" inside "ClockTimer"), so it actually doesn't need it.
I'd suggest the following:
Remove the "clockFrame" parameter and field inside "ClockTimer"
Create the "timer" object first
Create the "analogClock" object second, and pass "timer" to it

Create an object from the contents of a variable java

How do I Create an object from the contents of a variable in Java?
For example: if the string variable "name" has a value of "Margaret"
and I apply this constructor
Name nx= new Name();
I want Java to know that I´m refering to the content of name. Therefore Java will know that I'm refering to Margaret
What I want is to create Dynamic objects without the name fixed.
I can do this in PHP but I'm new in Java and don't know if is it possible.
Thanks
Dynamic instantiation of Java classes is far more complicated than doing the same thing in PHP. In PHP you'd just use eval(), i guess.
You can get an instance of the Class object by using Class.forName. Afterwards, you would have to instantiate the object by using the Class.newInstance:
Class cls = Class.forName(name);
Object obj = cls.newInstance();
This is the simplest case. If the constructor needs parameters, you would have to get the constructor method and call it.

Passing a class instance into another class

I've looked everywhere but I can't seem to find an answer to this.
If I have one class (say, a Resource class) and another class (say a Sprite class) and each time I create a sprite, I pass in a reference to my Resource class (because it's required for some function) - am I correct in assuming, that all this does is creates a reference to this instance of said class?
So - if my Sprite constructor is this:
public Sprite(Resource res){
res.doSomething........
}
And I create 100 sprites, then this isn't going to cause problems because it's just passing in a reference or 'pointer'? (as opposed to creating a new instance each time).
Simple enough question I know, but I want to make sure I understand what's happening here and I couldn't find the answer to this anywhere.
Yes, if you create a new Sprite and pass it an existing Resource, the new Sprite will just have a reference to your original object.
So 100 Sprites would have 100 references to your 1 Resource.
Yes you have to create eachtime a new instance of the class to call doSomething() of that particular instance.

Is it possible to make a list of classes without instantiating them as actual objects?

I have a set of classes in my program which can be instantiated. Each class has a static method associated with it which updates a particular aspect of that class.
At run time the program chooses to use a number of the classes from the set.
Multiple versions of these classes are then instantiated as objects as the program runs.
Throughout the program there are key points where I need to call the static methods associated with the various classes but only the ones in use that the program picked at run time.
Is it possible for me to make a list/array containing the classes in use without instantiating them? So that I can then just go through that list and call the static method on each class?
The only other way I have found of doing it is instantiating one object for each class in use and storing them in an array and calling the methods from there. This seems a bit inefficient and not very elegant.
Any help is greatly appreciated.
You could use reflection to do something like this:
List <Class<?>> classes = new ArrayList<Class<?>>();
// alternatively, classes.add(com.foo.MyClass1.class);
classes.add(Class.forName("com.foo.MyClass1"));
classes.add(Class.forName("com.foo.MyClass2"));
// ...etc
Method m = classes.get(0).getDeclaredMethod("staticFunction");
m.invoke(null); // pass in an instance of the class if this is an instance method
Check out the javadoc for Class and Method.
I'm not sure why you want to avoid instantiation, but if the only thing you are doing is calling static methods, there is not reason to create objects. As for your question, you can create an Array of Classes without any problem. Remember that MyClass.class is actually an object of class Class, so this would work :
Class[] classes = new Class[] {MyClass1.class, MyClass2.class};
You can then use your logic to select some of them. When done, you can instantiate them using reflection :
classes[0].newInstance();
Hope it helps, if not, please precise your question. Note that the class objects themselves will be created (not sure you can prevent the class loader to load them).
You can have a list of Class objects such as
List<Class> myClasses = new ArrayList<Class>();
To add an item to the list you code something like myClasses.add(Class1.class);
That's one way to store a list of classes, without needing to create an instance of each one.

Get access to the ORB into an implementation?

I have a tricky problem. And being new to CORBA, I'm unable to get out of it.
How can I instantiate an implementation object from another implementation?
Usually, if I have an interface A. I would create an A_Impl class (in a A_Impl.java file), extending from the A_POA class generated from the idl.
Then, on the server side I would do something like this :
AImpl my_a_impl = new A_Impl ();
org.omg.CORBA.Object ref = orb.activate_object(my_a_impl);
A my_a_object = A.narrow(ref);
But what when one of the methods of another object B needs to return A?
In my B_impl class, I don't have access to the orb and thus cannot get a reference to my object by using the activate_object method.
How can I then return such an object?
Any help would be greatly appreciated.
Thanks in advance !
CORBA already offers a function that can be used to get an existing ORB reference - ORB_init(). For most ORBs, that function behaves like a singleton and will return an existing ORB object, assuming you use the same ORB ID each time you call it.
Put a reference to the current active orb in a singleton class, that you can reach from very object *_Impl.
It seems like (for a non comprehensible reason for me), constructors are not generated by the idl.
Creating constructors allows me to simply pass objects as references and make them visible to my classes.
Yes... as simple as that :)

Categories

Resources