473,376 Members | 1,043 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,376 software developers and data experts.

Instantiating a Class

How do you instantiat a class with a Class.forName( ) method, if the class's
default constructor requires a parameter?
Jul 17 '05 #1
2 21904
"Vijay Singh" <vi*******@hotmail.com> wrote in message news:<ef****************@news-binary.blueyonder.co.uk>...
How do you instantiat a class with a Class.forName( ) method, if the class's
default constructor requires a parameter?


The Class.forName(...) method only loads the class and executes any
static initializers there may be. It does not instantiate any objects
of the class (except if a static initializer does that). If you write
Class.forName("X").newInstance(), however, that will create a new
instance of the class, using the constructor with zero parameters.

You can do the following

You can only use the default constructor however (the one with
no parameters), if you wish to pass in parameters you must
create an init() method (or similar) for the class being created
and then you can call it as:

Class class = Class.forName("MyClass");
Object object = class.newInstance();
MyClass myClass = (MyClass) object;
myClass.init(int myParameter, MyOtherClasses myParameter2);

Hope this helps,
Daniel Goldman
http://d-goldman.org
Jul 17 '05 #2
D Goldman wrote:
"Vijay Singh" <vi*******@hotmail.com> wrote in message news:<ef****************@news-binary.blueyonder.co.uk>...
How do you instantiat a class with a Class.forName( ) method, if the class's
default constructor requires a parameter?

The Class.forName(...) method only loads the class and executes any
static initializers there may be. It does not instantiate any objects
of the class (except if a static initializer does that). If you write
Class.forName("X").newInstance(), however, that will create a new
instance of the class, using the constructor with zero parameters.

You can do the following

You can only use the default constructor however (the one with
no parameters), if you wish to pass in parameters you must
create an init() method (or similar) for the class being created
and then you can call it as:

Class class = Class.forName("MyClass");
Object object = class.newInstance();
MyClass myClass = (MyClass) object;
myClass.init(int myParameter, MyOtherClasses myParameter2);

Hope this helps,
Daniel Goldman
http://d-goldman.org


If your class Foo has a constructor like, say

Foo (int i, String s) {...}
You might use
Class cfoo = Class.forName("Foo");
Constructor con = cfoo.getConstructor(Integer.TYPE, String.class);
Object o = con.newInstance(new Object[]{new Integer(27), "Hi !"});
Foo foo = (Foo)o;

add appropriate exception handling :)

Soren

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: pesso | last post by:
I have an array of filenames, which are paths to the .NET assembly DLLs I built. I know for sure that these assemblies have public method, Execute(). In the run-time, I want to be able to...
2
by: FredC | last post by:
S Name Microsoft Windows XP Professional Version 5.1.2600 Service Pack 2 Build 2600 Total Physical Memory 1,024.00 MB MDE 2003 Version 7.1.3008 ..NET Framework 1.1 Version 1.1.4322 SP1...
2
by: HarishP | last post by:
Hi, How to avoid instantiating the class more than 10 times Harish.P Sr. Software Engineer Comat Technologies Pvt. Ltd., Bangalore Email: harish.p@comat.com
2
by: active | last post by:
Because of an example I followed I've been instantiating Image objects. Now I notice that the documentation says the Image class is an abstract base class. Things seem to be working! Is the...
6
by: Gary Frank | last post by:
What are the ramifications if I were to instantiate an object tens of thousands of times and add them to an array? Or hundreds of thousands of times? Do you know if the act of instantiating a...
1
by: Marja Ribbers-de Vroed | last post by:
I've been provided with a custom ActiveX DLL (written in C++) that reads a certiifcate to generate a signature for a passed XML string, and I'm having trouble instantiating it. The DLL is registered...
3
by: Nagesh | last post by:
hi, I have seen the winvnc(tightvnc server) source code in this I seen that class member funtions are calling without instantiating the object i.e. like vncService::ShowDefaultProperties() where...
1
by: Bruce | last post by:
I am getting the following exception in the release build of my assembly. {"Attempted to read or write protected memory. This is often an indication that other memory is...
3
by: Randy | last post by:
Hi, I was learning about RTTI when I ran across this example. This line, out of the example, confused me. It is declaring a pointer to a base type and instantiating it with a derived class. I...
18
by: RB | last post by:
Hi guys (and gals!), I've got 2 classes, "TypesafeConstant" and "Color". "Color" inherits from "TypesafeConstant", and adds no new functionality. All "Color" does is to instantiate some class...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.