constructor « Class.forName « Java Class Q&A

Home
Java Class Q&A
1.abstract class
2.Base class
3.class hierarchy
4.class name
5.class version
6.Class.forName
7.ClassCastException
8.Clone
9.constant
10.Constructor
11.Development
12.DTO
13.encapsulation
14.equal method
15.extend Class
16.getter
17.hashcode
18.Inheritance
19.inner class
20.interface
21.main class
22.Method
23.NoClassDefFoundError
24.NoSuchMethodError
25.NoSuchMethodException
26.object reference
27.overload
28.parent class
29.Polymorphism
30.private
31.Private Field
32.Recursive
33.setter
34.Static
35.Static Class
36.subclass
37.Super
38.toString
39.Wrapper Class
Java Class Q&A » Class.forName » constructor 

1. Instantiate singleton object using Class.forName()?    stackoverflow.com

I want to instantiate one instance of a class from the string name of the class. ( using Class.forName().newInstance(). ) Here's the problem: I want that instance to be a singleton.. I could do ...

2. Intializing a class with Class.forName() and which have a constructor which takes arguments    stackoverflow.com

I am instantiating a class like this.

myObj = (myObj) Class.forName("fully qualified class name here").newInstance();
My doubt here is if we have a constructor which takes arguments how can we instantiate it like ...

3. Using Class.forName() with constructor arguments    coderanch.com

Class.forName() load a class; I think you're thinking of Class.newInstance() which creates an instance of that class. But the answer to your question is absolutely "yes". You can call the "getConstructors" method of the Class object returned from Class.forName() to get a list of java.lang.reflect.Constructor objects. This class has a newInstance() method that lets you pass arguments to the constructor.

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.