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.
Related
I want to create an object in java:
MyObject obj = new MyObject ();
and I want to pass it to prolog with a jpl query.
How can I accomplish java to prolog object passing?
I know that I could use jpl_new in a prolog file like this:
execMethod :-
jpl_new('my_package.MyObject', [], Object),
jpl_call(Object, myMethod, [], _ ).
But, I want to avoid the jpl_new call and just use the jpl_call with the java object obj.
And converserly,
How can I accomplish prolog to java object passing?
I mean passing to java, objects created with a jpl_new call.
In other words, I want to share an object state between java and prolog.
To access a Prolog knowledge base from within Java, you can use JPL Queries. Let's look at a simple, trivial example below:
% Knowledge base (Prolog)
foo(x,bar).
all_foo(X,Y) :- foo(X,Y).
In java, we could then write:
String query = "all_foo(x,Y)";
System.out.println("First solution: " + Query.oneSolution(query).get("Y"));
which would return 'bar' as answer in Y.
Vice versa -as you showed in your question- JPL can be used when we want to access Java functionality from within a Prolog file.
Firstly, looking at the docs of jpl_call/4, we see that its first arguments can be:
a type, class object or classname (for static methods of the denoted class, or for static or instance methods of java.lang.Class)
a class instance or array (for static or instance methods)
So you are free in how to pass your class information to jpl_call/4 to execute certain methods.
Subsequently, you can access your Java model rather than executing logic by using jpl_get/3. An example below is shown where we bind the Prolog variable Colour to a reference of a field of a Java car object held in the static final .colour field of the example.class.car class.
jpl_get('example.class.car', colour, Colour)
More generally:
jpl_get(+Class_or_Object, +Field, -Datum)
Hope this helped.
Good luck!
In java it is possible to cast an object onto a class.
An good example is found here
Object aSentenceObject = "This is just a regular sentence";
String aSentenceString = (String)aSentenceObject;
I have a program that needs to integrate some java with python. I am trying to do this via the JCC library. The problem that I am encountering is that with JCC, all of the java classes are loaded into the imported library that I created with JCC. So I can create an instance of the base class by passing the necessary argument to the constructor of the java class.
obj = javaLibrary.BaseClass('foo')
However, in my code I need to be able to cast this object onto a a more “specific” type of Object.
How can I accomplish this in python with JCC? It seems like it may be impossible because python is dynamically typed, but that is why I am asking this question.
All comments above valid, but to be specific for your case:
casted_obj = Object.cast_(obj)
I want to read a flat file with contents like "Deepak,25,Singhal" and then populate the POJO dynamically. I got the method object using reflection. I also created an instance using newInstance().
Problem I am stuck is method.invoke( obj, Object ..) ..Now how do I determine the Type of argument to be passed ! From Method object how do I know that argument to be passed is int; so that I could convert String 25 to corresponding type so that method.invoke could be invoked !
Use a java class with the help of Regular expression to identify Strings and number sand convert to respective type generically
How would I get a variable in another class using a variable in the current class? For example: The variable "userclass" Can either be human or alien. Inside my other class ("Cv.java") there are two variables (human, alien) How would I get one of the two variables in Cv.java, while using the "userclass" variable to get it.
Example 2:
userclass = alien
Cv.????
How would I get Cv.alien whilst using "userclass"
You can use reflection to do it, although it might hurt the performance.
A better way is to use a Enum to represent the userclass instead of using a String.
how about using class.forName :
Object o = Class.forName(userclass)
this assuming your userclass is a string.
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();