-3

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
user3738243
  • 159
  • 2
  • 13

5 Answers5

1

Just add a print message in each constructor or use a debugger

Ezra
  • 1,401
  • 5
  • 15
  • 33
1

You could add a "flag" to your class (or use Integer and check for c nullity):

class Classname{

  private int constructor; // the flag
  private int a, b, c;

  private Classname(int a, int b){
     this.constructor = 1;
     this.a = a;
     this.b = b;
  }

  private Classname(int a, int b, int c){
     this.constructor = 2;
     this.a = a;
     this.b = b;
     this.c = c;
  }
}

Then use this flag:

public static String getStringform(Classname classname){
    String stringform;
    if(classname.constructor == 1) {
       stringform = "" + classname.a + classname.b;
    } else {
       stringform = "" + classname.a +classname.b +classname.c;
    }
    return stringForm;
}

NB: I added "", braces and the return statement in getStringForm, but there still might be issues (access to private members, private constructors, ...)

0

The answer is simple, if there is no constructor specified in given class then default constructor is used i.e. constructor with no argument like this private Classname(){...}, now you have 2 constructor lets call it C1 as private Classname(arg1, arg2){...} and C2 as private Classname(arg1, arg2, arg3){...} so if you create class object with 2 integers passed while creating object then C1 will be used and while passing 3 integers C2 will get called.

Make a note here, as you have created C1 and C2 so default constructor is not available to this class anymore. If you want to use that too create that constructor too.

Rupesh
  • 2,627
  • 1
  • 28
  • 42
0

You might have a modelling problem here.

I'd simply would have two distinct classes, here is the pseudo-code:

public class ClassWithTwoFields {
  public final Object field1;
  public final Object field2;

  public ClassWithTwoFields(final Object f1, final Object f2) {
    field1 = f1;
    field2 = f2;
  }

  public String getStringform() {
    return field1 + field2;
  }

}

public class ClassWithThreeFields extends ClassWithTwoFields {
  public final Object field3;

  public ClassWithThreeFields(final Object f1, final Object f2, final Object f3) {
    super(f1, f2);
    field3 = f3;
  }

  public String getStringform() {
    return field1 + field2 + field3;
  }
}
Psyx
  • 758
  • 1
  • 9
  • 15
  • No, the constructor for both two must be the same or I can't make object[] containing both; two and three fielded objects – user3738243 Jan 26 '15 at 13:26
0

With the current class each of the member fields will (before the constructor is run) be initialised with 0 (the default value for an int field). So if the constructor doesn't initialize c it will be zero.

Now, this may be a valid value for c that can be passed into the 3-argument constructor that does set c, so you can't simply check if c is 0.

You could solve this issue several ways. The one that makes sense will depend on what the fields actually mean. But essentially it boils down to recording whether or not c has been set to a valid value.

This could be as simple as defining a boolean field called cIsValid and setting it to true when c is set in the constructor and false if not.

Mike Tunnicliffe
  • 10,674
  • 3
  • 31
  • 46