473,397 Members | 2,033 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,397 software developers and data experts.

How do I pass parameter when using newInstance

Hello!

I use method forName and newInstance in class Class like this
instance = (GameFactory)(Class.forName(name).newInstance() );

Now to my question:
Assume that name is GameFactory then I want to pass a parameter to the
GameFactory constructor
like this instance = (GameFactory)(Class.forName(name).newInstance(fiel d) );
Here I pass parameter field which is an int but when I do so I get compile
error saying

F:\gameLab\GameFactory.java(15): Cannot find method 'newInstance(int)' in
'java.lang.Class'

How you any idea?

Or is it so that if I want to pass parameter I have to explicitly use new
GameFactory(field);

//Tony



Oct 19 '05 #1
2 23957

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:Ld*********************@newsc.telia.net...
Hello!

I use method forName and newInstance in class Class like this
instance = (GameFactory)(Class.forName(name).newInstance() );

Now to my question:
Assume that name is GameFactory then I want to pass a parameter to the
GameFactory constructor
like this instance =
(GameFactory)(Class.forName(name).newInstance(fiel d) );
Here I pass parameter field which is an int but when I do so I get compile
error saying

F:\gameLab\GameFactory.java(15): Cannot find method 'newInstance(int)' in
'java.lang.Class'

How you any idea?

Or is it so that if I want to pass parameter I have to explicitly use new
GameFactory(field);


In short, you can't do this. From the JavaDocs:

<quote>
Creates a new instance of the class represented by this Class object. The
class is instantiated as if by a new expression with an empty argument list.
</quote>

- Oliver
Oct 19 '05 #2
"Tony Johansson" <jo*****************@telia.com> wrote in
news:Ld*********************@newsc.telia.net:
Hello!

I use method forName and newInstance in class Class like this
instance = (GameFactory)(Class.forName(name).newInstance() );

Now to my question:
Assume that name is GameFactory then I want to pass a parameter to the
GameFactory constructor
like this instance =
(GameFactory)(Class.forName(name).newInstance(fiel d) ); Here I pass
parameter field which is an int but when I do so I get compile error
saying

F:\gameLab\GameFactory.java(15): Cannot find method 'newInstance(int)'
in 'java.lang.Class'

<snip>

I don't think that you can do this with Class, but you can use the reflection
package java.lang.reflect . Use Class.getConstructor or
Class.getConstructors to get an appropriate Constructor. Then use
Constructor.newInstance to create and initialize an instance.

It is going to be more complex than the no-argument constructor that you were
using before.

Good Luck!
--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
Oct 19 '05 #3

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

Similar topics

3
by: Nelson Broat | last post by:
In jsp land you can have the following: <% String name = "Nelson"; %> Hi, my name is <%= name %>. Such that, in your browser you see:
0
by: Frank | last post by:
Hey all, I can't seem to get javascript running in my XSL document. <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"...
7
by: Zlatko Matić | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
8
by: MrEd | last post by:
Hi all, I need to pass a class to a method, (not an instance of a class), I'll explain... lets say we have a class, lets call it Shape (as the tipical example), then lets say that i also have...
5
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into...
1
by: menmysql | last post by:
hi to all, in my jsp page hyper links are there starting from "A" to "Z". when ever i click on any one of those links it should call itself and pass a parameter of its value. for example...
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
2
by: rekhaagroya | last post by:
In some jsp I wanted to pass an array variable in the method doValidate of ValidationApplet. I have defined a variable in the function validateAll() as below, var comments_arr =...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.