Linked Questions

49 votes
2 answers
67k views

How to create instance of a class with the parameters in the constructor using reflection? [duplicate]

for example: public class Test { public static void main(String[] args) throws Exception { Car c= (Car) Class.forName("Car").newInstance(); System.out.println(c.getName()); } ...
user471011's user avatar
  • 7,406
1 vote
2 answers
2k views

Why newInstance throws InstantiationException in my code? [duplicate]

I have two process as shown below. Each of my process has run and shutdown method Process processA = new ProcessA("processA", getProcessAProperties()); Process processB = new ProcessB("processB", ...
john's user avatar
  • 11.8k
178 votes
11 answers
485k views

How do I pass a class as a parameter in Java?

Is there any way to pass class as a parameter in Java and fire some methods from that class? void main() { callClass(that.class) } void callClass(???? classObject) { classObject.somefunction ...
user avatar
66 votes
7 answers
24k views

Writing a single unit test for multiple implementations of an interface

I have an interface List whose implementations include Singly Linked List, Doubly, Circular etc. The unit tests I wrote for Singly should do good for most of Doubly as well as Circular and any other ...
ChrisOdney's user avatar
  • 6,444
1 vote
5 answers
6k views

Create different objects depending on type

I have a database table that contains a column named type. For every row in my database column I have to create an object depending on the type. At the moment I use if else statements for that: if (...
J. Maes's user avatar
  • 6,912
2 votes
4 answers
2k views

Use the command line to make new objects

In my program, the user needs to input what type of players the game will have. The players are "human", "good" (for a good AI), "bad" (for a bad AI) and "random" (for a random AI). Each of these ...
Kyefer's user avatar
  • 98
2 votes
1 answer
4k views

Is it possible to create an instance of parameterized class in Java?

I have the same logic in classes creation, but actual classes are different. Is it possible somehow to create an instance of a parameterized class? Here is an example: public class MyClass <E ...
Oleksandr's user avatar
  • 3,784
2 votes
3 answers
667 views

Wrong results with Junit parameterized

I changed a junit test class to run with Parameterized to test two different implementations of the same interface. Here it is : @RunWith(Parameterized.class) public class Stack_Tests { private ...
dblouis's user avatar
  • 608
4 votes
1 answer
1k views

Create different Java classes based on runtime config text

I have a suite that can run a few operations with different parameters. The operations and their parameters are provided in an XML config file. There is a separate class implementing each operation. ...
Mikhail Ramendik's user avatar
2 votes
1 answer
1k views

How should I select which concrete implementation should be instantiated based on the user choice?

I have an interface Fruit with two implementations Apple and Banana. I want to create a Fruit instance. The choice whether the concrete implementation should be an Apple or a Banana should be made by ...
Stefan Dollase's user avatar
0 votes
2 answers
1k views

java instantiating object without knowing which subclass it will belong to until runtime

Scenario: I have a hierarchy of subclasses all inheriting from a single distance ancestral class. All super classes in the hierarchy are abstract, so the only concrete classes are those without ...
user4240561's user avatar
-1 votes
1 answer
1k views

Android java unable to load class

hi am unable to load class in my android app , i can load basic class but when i Initialize context in that class then am not able to load it public class main { // Initalize context Context ...
user134929's user avatar
-3 votes
1 answer
418 views

Java calling method from generic class

Is something like that possible. List<?> myList = getMyList(); class cls = class.forname("com.lab.myClass"); cls = myList.get(0); cls.getValue(); Create an Instance with the fully-qualified ...
user3623194's user avatar
0 votes
2 answers
213 views

Instantiate Object with constructor arguments in a generic method

I am currently attempting to write a method which returns a new array of a generic type filled with objects of random values but I am struggling with the creation of the Objects. Let's say I have a ...
L.Spillner's user avatar
  • 1,772
2 votes
4 answers
127 views

Returning Interface instead Class<Interface>

I wrote my own classloader, which works with classes, which implements interface Plugin. But I can't cast Class to Plugin. What's wrong? public Plugin load(String pluginName, String ...
jenius's user avatar
  • 257

15 30 50 per page