Asked  7 Months ago    Answers:  5   Viewed   28 times

Is there a way to create an instance of a particular class given the class name (dynamic) and pass parameters to its constructor.

Something like:

Object object = createInstance("mypackage.MyClass","MyAttributeValue");

Where "MyAttributeValue" is an argument to the constructor of MyClass.



Yes, something like:

Class<?> clazz = Class.forName(className);
Constructor<?> ctor = clazz.getConstructor(String.class);
Object object = ctor.newInstance(new Object[] { ctorArgument });

That will only work for a single string parameter of course, but you can modify it pretty easily.

Note that the class name has to be a fully-qualified one, i.e. including the namespace. For nested classes, you need to use a dollar (as that's what the compiler uses). For example:

package foo;

public class Outer
    public static class Nested {}

To obtain the Class object for that, you'd need Class.forName("foo.Outer$Nested").

Tuesday, June 1, 2021
answered 7 Months ago

Missing indentation. Indent your code.


instance Fractional Physical where
(Physical v1 u1) / (Physical v2 u2) = (Physical (v1 / v2) (sqrt((u1 /v1)^2 + (u2 /v2)^2)*(v1 /v2)))


instance Fractional Physical where
  (Physical v1 u1) / (Physical v2 u2) = (Physical (v1 / v2) (sqrt((u1 /v1)^2 + (u2 /v2)^2)*(v1 /v2)))

-- even a single space would be ok


You actually define a new top level (/). Your actual Fractional instance is empty. If it wasn't for the ambiguous occurrences, you would get warnings for no explicit implementations.

Saturday, August 7, 2021
Sunny Shah
answered 4 Months ago

Because Integer doesn't have no-arg(default) constructor, class.newInstance() will invoke default constructor internally

Saturday, September 11, 2021
answered 3 Months ago

Paragraph 6.4.2 of the Java Language Specification has some information about the rules that apply in this case.

A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type, or a package. In these situations, the rules of §6.5 specify that a variable will be chosen in preference to a type, and that a type will be chosen in preference to a package. Thus, it is may sometimes be impossible to refer to a visible type or package declaration via its simple name. We say that such a declaration is obscured.

This refers to paragraph 6.5 Determining the Meaning of a Name, which explains the rules in detail.

In your example, Outer.Inner could refer to the type of the nested class named Inner, or the static member variable Inner. The rules say the variable will be chosen over the type.

Monday, September 27, 2021
answered 2 Months ago

This is how you do it:

 * Create a new instance of the given class.
 * @param <T>
 *            target type
 * @param type
 *            the target type
 * @param className
 *            the class to create an instance of
 * @return the new instance
 * @throws ClassNotFoundException
 * @throws IllegalAccessException
 * @throws InstantiationException
public static <T> T newInstance(Class<? extends T> type, String className) throws
        IllegalAccessException {
    Class<?> clazz = Class.forName(className);
    Class<? extends T> targetClass = clazz.asSubclass(type);
    T result = targetClass.newInstance();
    return result;

My my = newInstance(My.class, "some.common.prefix.MyClass");
Tuesday, November 9, 2021
Narendra Chitrakar
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :