I have a constructor which gets a HashSet and a HashMap. I need to run a validation check on one hashMAp and combine it with the hashSet, as 'super' must receive only one hashSet.
I can't find a way to do it as I get following error: cannot reference this before supertype constructor
Example:
public class A extends B {
public A(HashSet<Obj> h1, HashMap<UID,Objects> m1) {
super(new C (h1) ); //h1 should contain changes related to m1..
}
I want to do something like that:
public class A extends B {
public A(HashSet<Obj> h1, HashMap<UID,Objects> m1) {
runMyFunc(h1,m1);
super(new C (h1) );
}
runMyFunc(HashSet<Obj> h1, HashMap<UID,Objects> m1){
//do checks
//more checks...
// if something then h1.setUid(m1.get(0))...
return h1;
}
I thought converting the constructor to private and then run it like that:
public class A extends B {
private A(HashSet<Obj> h1) {
super(new C (h1) );
}
public A(HashSet<Obj> h1, HashMap<UID,Objects> m1) {
runMyFunc(h1,m1);
this(h1);
}
but it also didn't work.
Can you please advice?
Use of super() to access superclass constructor To explicitly call the superclass constructor from the subclass constructor, we use super() . It's a special form of the super keyword. super() can be used only inside the subclass constructor and must be the first statement.
With super(parameter list) , the superclass constructor with a matching parameter list is called. Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.
“this()” and “super()” cannot be used inside the same constructor, as both cannot be executed at once (both cannot be the first statement). “this” can be passed as an argument in the method and constructor calls.
The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.
Just make your runMyFunc
static, and invoke it as a function where you use the return value in the super
invocation. That is allowed:
public class A extends B {
public A(HashSet<Obj> h1, HashMap<UID,Objects> m1) {
// Invoke as function rather than by itself on a separate line
super(new C (runMyFunc(h1,m1)) );
}
// Make method static
public static HashSet<Obj> runMyFunc(HashSet<Obj> h1, HashMap<UID,Objects> m1) {
//do checks
//more checks...
// if something then h1.setUid(m1.get(0))...
return h1;
}
Make your method static
and make sure it returns your new h1
.
public static HashSet<Obj> runMyFunc(HashSet<Obj> h1, HashMap<UID,Objects> m1) {
// Some mutation to h1
return h1;
}
Use it in the first line as follows:
this(runMyFunc(h1,m1));
You must be wondering "Why am I able to use static methods and not instance methods?". Well, before you can call your methods, your parent's object has to be instantiated first, this is meant to help the compiler prevent you from accessing attributes/methods/whatever that are not yet available. Static methods are safe cause by definition can't access any of that.
Why does this() and super() have to be the first statement in a constructor?
Constructor call must be the first statement in a constructor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With