I'm not quite sure if the name of the thing that I'm describing is called constructor? However what I want to do is to see which constructor is used to create object.
Class Classname{
private int a, b, c;
public Classname(int a, int b){
this.a = a;
this.b = b;
}
public Classname(int a, int b, int c){
this.a = a;
this.b = b;
this.c = c;
}
}
And if we create object:
Classname testobject = new Classname(1, 2);
What I need that for is because I need to get the number of varuables in the object and the values of those variables. Let's say I've the following:
classname[] multiple = new classname[10]
That has "classname" -objects from both constructors and I need to write those objects into Strings so I think that I must somehow get the right constructor and if it's the first one
public static String getStringform(Classname classname){
String stringform;
if(constructor == 1)
stringform = String.valueOf(classname.a) +String.valueOf(classname.b);
else
stringform = String.valueOf(classname.a) +String.valueOf(classname.b) +String.valueOf(classname.c);
}
Edit: As suggested in the comments, I made new constructors like this:
private int type;
private int a, b;
public Classname(int a, int b){
this.type = 1;
this.a = a;
this.b = b;
}
but even with that kind of constructor when I'm trying to get the "type" of an object made in another class, I don't get the right type..? I get java.lang.NullPointerException when using the followin:
public static String toString(Classname object){
if(object.type == 1) //<-- There's the exception