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.
Related
How to get number of methods and variables in a class Swift iOS like reflection in Java. Something like getDeclaredMethods() and getDeclaredFields().
getDeclaredMethods() //will return array of methods defined.
getDeclaredFields() //will return array of variables defined.
You use the Mirror(reflecting: Any)and then loop through the mirror.children
This however wont tell you any information about the functions inside it. the child only contains the variable names and values.
i guess This will be a sufficient answer tho.
I have two classes A and B
A class contains a method which has some of the local variables.
now I want to fetch a particular variable's data type in the B class.
can you please help me with this.
I researched about java reflection. but I found that I can not achieve this by using reflection, as local variables are stored in stack at the runtime. And reflection can only fetch instance or class variables.
for example,
class A{
method1(){
variable1;
}
}
class B{
method 2(){
fetch variable1;
}
}
I want the data type of variable1 as a result.
Is there any way to achieve it using mocking or something?
thanks for your support.
I found the answer myself.
I can do this via Java Regex Concept.
As I know a little about the variable's naming pattern,
So I am planning to parse the class1.java file and first will try pattern matching the method name and then the variables name.
So that I will get the line on which the variable is declared.
for example, if my variable is object,
String abc=new String();
I will try regex like, String regex=".=new.().*"
So this will fulfill my purpose.
Thanks again for helping.
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.
I have a doubt about which option would be a better one in order to have a more understandable code. I have a variable that will be used just inside one method but, as this variable is a configuration variable, I think that it would be nice that this would be a global one. So I have created it on the top of the class as:
private final int VARIABLE = 5000;
But then, as this variable is used just once, the Android Studio launches a warning that says "Field can be converted to a local variable". Do you think that is better to keep this variable as a global one (in order to be more clear for the future) or it is better to set it as a local variable?
Thanks in advance!
Keep all your variables which are not going to change, for example base url strings etc in a separate java file in a separate package like Utils. Place all your variable there and make them static so that you can directly access them by using the class name.
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!