Is there any way to get an Object from a Class<?>
public static Object getObject(Class<Object> type)
{
// returns a new Object of whatever i passed in the parrameter
}
Something like that
Printable View
Is there any way to get an Object from a Class<?>
public static Object getObject(Class<Object> type)
{
// returns a new Object of whatever i passed in the parrameter
}
Something like that
Hello, welcome to the forums
The way i would do this is something like:
And then call it from another class by:Code:public class Class1{
public double someNumber(double dblNum)
{
return dblNum;
}
}
I'm not entirely sure if this is what you wanted but at least it can give you some idea.Code:public class Class2{
public static void main (String args[])
{
Class1 c1 = new Class1();
double number = c1.someNumber();
}
}
Regards,
No. not possible, because some objects just don't have the default constructor (The one with no parameters). Java IS NOT a dynamic language.Quote:
Originally Posted by WigglyWhomp
yes,you can use this code but you have to pass double type value in your code.
place the value where i have used abc.
eg.. Class1 c1 = new Class1();
double number = c1.someNumber(abc);
then it can work properly.
oh ye, i think wat ishu is saying something like:
im not 100% certain that my code shown above will giv you some error. It should give you an idea. This or my suggestion on my first post should do it.Code:
public class Class2
{
public int intMethod(int i)
{
//do some processing here
return i;
}
}
public class Class1
{
public void test()
{
Class2 c2 = new Class2();
int x;
//process x
int y = intMethod(x);
}
}