Pass any Class as a parameter for a method

java pass generic class as parameter
java pass class type as parameter
java pass class as parameter and instantiate
passing object as parameter in java example
how to pass class name as parameter in java
pass class as parameter kotlin
java pass dynamic class as parameter
pass constructor as argument java

This has probably been asked before and I've seen a few similar/along the lines, but didn't quite understand it.

In Java, how do make it so a method accepts any class as a parameter?

For example,

public (AnyClass) myMethod(String whatever, (AnyClass) thisClass){

}

to be used like:

myMethod("Hello", ClassA);
myMethod("Hello", ClassB);

Thanks for any help!

Edit:

Usage example was asked for; What I'm doing is mapping a JSON string to a POJO, but trying to abstract this whole thing out so it can be reused. So - I need to be able to pass through any class, and the JSON string (or HttpConn), then build then use Jackson to build that POJO and return the object, of whatever type that may be.

(An idea of what I want to do):

public Class<?> doSomething(String jsonString, Class<?> clazz){
    clazz newInstance = mapper.readValue(jsonString, clazz);
    return clazz;
}

called like:

ClassA test = doSomething("String One", ClassA.class);
ClassB testTwo = doSomething("Different format string", ClassB.class);

Hope that helps understanding of the problem... and my understanding of the solutions!

How do I pass a class as a parameter in Java?, When you invoke a method, the arguments used must match the declaration's parameters in type and order. Parameter Types. You can use any data type for a​  Hi All, I am going to create a method which will require any custom class reference (not object)to Execute. class A { } class B { } class C { pubilc void RunTimeMethod(Parameter) // Here I want to accept Class A or Class B as parameter at runtime { // Method is working fine if enter I use RuntimeMethod(A obja).

I assume you need to accept a class of some type T as a parameter and return that class as well. In that case, your method signature will be something like this:

public <T> Class<T> myMethod(String whatever, Class<T> clazz){
    // put the rest of your code here
}

Passing Information to a Method or a Constructor (The Java , When we pass a primitive type to a method, it is passed by value. But when we Defining a constructor that takes an object of its class as a parameter. One of the In java, a method can return any type of data, including objects. For example  Basically, you can pass any object as a parameter to a method. But, having said that, first, we would need to learn how to store a function in an object. Simplest example of Higher Order Function (passing one function to another as an parameter or argument) in Java 8!!! Click To Tweet.

1) Without type information:

public static Class<?> myMethod(String text, Class<?> clazz) {
    // body
    return clazz;
}

Client code:

public static void main(String[] args) {
    Class<?> clss = myMethod("Example", String.class);
}

2) With type information:

public static <T> Class<T> myMethod(String x, Class<T> clazz) {
    // body
    return clazz;
}

Client code:

public static void main(String[] args) {
    Class<String> clsStr = myMethod("Example", String.class);
    Class<?> clsAny = myMethod("Example", String.class);
}

3) Raw type (will work but NOT recommended):

public static Class myMethod(String x, Class clazz) {
    // body
    return clazz;
}

Client code:

public static void main(String[] args) {
    Class<String> clsStr = myMethod("Example", String.class); // warning for Unchecked Assignment 
    Class<?> clsAny = myMethod("Example", String.class);
    Class clsRaw = myMethod("Example", String.class);
}

4) With type information defined at class level:

public class Main<T> { 

    // cannot be static
    public Class<T> myMethod(String x, Class<T> clazz) {
        // body
        return clazz;
    }
}

Client code:

public static void main(String[] args) {
    Main<String> m = new Main<>();
    Class<String> cls = m.myMethod("x", String.class);
}

Passing and Returning Objects in Java, Any modifications to the formal parameter variable inside the called function or method affect only the separate storage Passing variables in the class function. Passing Primitive Data Type Arguments. Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost.

Parameter Passing Techniques in Java with Examples , We can pass Object of any class as parameter to a method in java. We can access the instance variables of the object passed inside the called  A parameter is a value that you can pass to a method in Java. Then the method can use the parameter as though it were a local variable initialized with the value of the variable passed to it by the calling method. The guessing-game application has a method named getRandomNumber that returns a random number between 1 and 10:

Java passing object as parameter - Java Tutorials, As you can see the return type changes depending on what class object you pass in as parameter to the method. This can be quite handy in  Value Parameters It is a normal value parameter in a method or you can say the passing of value types by value. So when the variables are passed as value type they contain the data or value, not any reference. If you will make any changes in the value type parameter then it will not reflect the original value stored as an argument.

Java Generics - Class Objects as Type Literals, In the main method we are instantiating the Student class and making a copy by passing it as an argument to the coypObject() method. The following example demonstrates passing value-type parameters by value. The variable n is passed by value to the method SquareIt. Any changes that take place inside the method have no effect on the original value of the variable. class PassingValByVal { static void SquareIt(int x) // The parameter x is passed by value.

Comments
  • Any Class object or any instance? For the former use Class<?> for the latter use Object
  • No it has to be the class itself - or class reference to be used later (for mapping). Is that possible?
  • This may help you.stackoverflow.com/questions/4872978/…
  • In that case you can just use Class<?> as the parameter type and then call it like: myMethod("Hello", ClassA.class)
  • Yeah it does, is that an issue?
  • Thanks a lot for the explanation and help! This is just about the most coherent answer to this I've managed to find :)