0

I know this is a basic question but i would like to know how to solve this:

In a class we have a constructor as below:

public class Constructor{

private String a;
private String b;
private String c;

public Constructor(String a){
this.a = a;
this.b = getB(a);
this.c = getC(a,b);
}

public String getB(String a){
return a.trim();
}

public String getC(String a, String b){
return a + b;
}

}

When line this.c = getC(a,b) is called b is still null, how can we achieve this style of constructor?

Thanks.

9
  • "When line this.c = getC(a,b) is called a and b are both still null" they're only null if you pass in null as their values, e.g. new Constructor(null, null). Commented Feb 5, 2021 at 13:38
  • @AndyTurner Sorry i see what you mean i have made an edit to try to complicate the issue a little more Commented Feb 5, 2021 at 13:40
  • @ProjH the question is how you are calling your constructor or methods ? Commented Feb 5, 2021 at 13:46
  • Your IDE will warn you about an "overridable method call in constructor" (or at least your IDE should). When, in a constructor, you call a method which can be overridden by subclasses, the subclass has access to a not fully constructed object. Be advised! (More details here.) Commented Feb 5, 2021 at 13:48
  • Thanks, so how would you rearrange this to ensure that b exists before you run getC(), Is this not possible in a constructor. Do i have to separate these? Commented Feb 5, 2021 at 13:52

2 Answers 2

1

The only reason for b to be null in this case is that the getB method is returning null.

Maybe this code is based on some other real code that has some other behavior in it causing the null.

Sign up to request clarification or add additional context in comments.

1 Comment

This is correct, i have followed the code and there is a bug returning Null. So for anyone that is unsure as i was, the constructor design is sound and as pointed out by @ThomasKlager Constructors are executed strictly sequential.
0

Looking at the constructor you have wrote and according to what is called the "variables scope", the instance of "a" which you are passing to the methods "getB" and "getC" is NOT the class instance member, but it is the reference of variable "a" passed in input to the constructor, thus the class instance member "b" will be already initialized when the method "getC" is invoked.
Surely, if you pass a null value to the constructor (i.e. new Constructor(null);) you will always get a "NullPointerException", but if the variable "a" is NOT null, you will have eventually:

  • Instance class member "a" = reference of input variable "a"
  • Instance class member "b" = string returned by "a.trim() , where leading and trailing space will be cut.
  • Instance class member "c" = concatenation of "a" and "b"

Here is an example of what I said:

public class Constructor {

private final String a;
private final String b;
private final String c;

public Constructor(String a) {
    this.a = a;
    this.b = getB(a);
    this.c = getC(a, b);
}

public String getB(String a) {
    return a.trim();
}

public String getC(String a, String b) {
    return a + b;
}

public static void main(String[] args) {
    Constructor myConstructor = new Constructor(" Hello ");
    System.out.println(myConstructor.a);
    System.out.println(myConstructor.b);
    System.out.println(myConstructor.c);
    new Constructor(null);
}

}

The result will be:

 Hello 
Hello
 Hello Hello
Exception in thread "main" java.lang.NullPointerException
at Constructor.getB(Constructor.java:15)
at Constructor.(Constructor.java:10)
at Constructor.main(Constructor.java:27)

1 Comment

Thank you @MarcoTizzano as i have mentioned above you are correct the constructor is sound i have followed the code and there is a bug returning Null. So for anyone that is unsure as i was, the constructor design is sound and as pointed out by Thomas Klager Constructors are executed strictly sequential.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.