Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a function before calling super in java?

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?

like image 929
user1386966 Avatar asked Jan 29 '17 13:01

user1386966


People also ask

How do you call a superclass method in Java?

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.

Does Java automatically call super?

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.

Can we use this () and super () in a constructor?

“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.

What does calling the super () method do in Java?

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.


2 Answers

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;
  }
like image 196
Erwin Bolwidt Avatar answered Nov 05 '22 19:11

Erwin Bolwidt


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));

But why?

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.

Related posts

  • Why does this() and super() have to be the first statement in a constructor?

  • Constructor call must be the first statement in a constructor

like image 36
Anis LOUNIS Avatar answered Nov 05 '22 20:11

Anis LOUNIS