Discovering a java object's construtor

Associate
Joined
19 Jun 2006
Posts
162
Location
Swansea, Wales
I have a Java class with two constructors. One accepts a string and 2 vectors, the other accepts a string and a vector.

Code:
/* Constructor 1 */
public P(String[] pSL, Vector pHd) {
}

/* Constructor 2 */
public P(String[] pSL, Vector pHd, Vector pBd) {
}

My question is simply this. How can I tell what constructor has been used to create my object?

I need to know because I only want to call a certain function if the second constructor has been used.
 
Dj_Jestar said:
then only call the method from within the second constructor :\

A little more explanation may be necessary...

Class Sniff sniffs a packet from the network and casts it as an object of type packet.

A method then analyses this packet and uses one of the two constructors to make an object based on the packet contents.

Sniff then calls a print function for the two or three parts of the object. If no third part has been created then obviously an error is thrown.

Thinking about it i could just catch the error and do nothing but is this good practice?
 
If Vector pBd is assigned to a instance variable you could just check to see if pbB is not null if it is execute the code for that, else execute the other code.

You can have if statments in toString() methods you know. :p
 
:( I was expecting some fun with reflection when I read the thread title.

As said before, set a boolean variable if the method can be run in the constructor and then check it before calling.
 
You say a string is passed into the constructor, but I assume you know you have an array of strings in the parameter list?
 
Sorted now. Stupid question really now I think about it but I'd been coding all day yesterday and I guess I was just having one of those coders block moments!

Dfhaii said:
:( I was expecting some fun with reflection when I read the thread title.

What the hell is reflection!? Is it as exciting as it sounds?
 
Wimnat said:
What the hell is reflection!? Is it as exciting as it sounds?

Reflection is an incredibly useful bit of kit for creating, modifying, and inspecting objects dynamically at run time.
 
Dfhaii said:
Reflection is an incredibly useful bit of kit for creating, modifying, and inspecting objects dynamically at run time.

Is it that useful?

Generally excessive use of reflection is a sign that your design is in some way flawed, IMO.
 
Visage said:
Is it that useful?

Generally excessive use of reflection is a sign that your design is in some way flawed, IMO.
There are some designs where Reflection, and lots of it, are works of genius. Not everything is an a->b scenario, so you can't create a glorified script for it; which is all tightly controlled objects are - glorified scripts.
 
Back
Top Bottom