0

I have such java class:

public class FirstTypeMD extends BaseParams {
    Integer framePerSec;

    public FirstTypeMD(Integer energyCons, Integer mass, Integer tempRange, Boolean electricProtect,
            Boolean radioProtect, Integer framePerSec) {
        super(energyCons, mass, tempRange, electricProtect, radioProtect);

        this.framePerSec = framePerSec;
    }


    public void setFramePerSec(Integer framePerSec) {
        this.framePerSec = framePerSec;
    }

    public Integer getFramePerSec() {
        return framePerSec;
    }

}

when I would like to create a new class object I have to do it in such way:

new  FirstTypeMD(energyCons, mass, tempRange, electricProtect, radioProtect, framePerSec)

But I would like to pass only first parameter for example like this:

new  FirstTypeMD(energyCons)

Maybe my general idea is wrong so I would like to describe what I want to do. I'm trying to filter array of objects of this class. I can do it in such way right now:

ArrayList<FirstTypeMD> resArr = new ArrayList<FirstTypeMD>(resArr1.stream()
                            .filter(c -> c.electricProtect == elProtect.getSelection()
                                    && c.radioProtect == radProtect.getSelection())
                            .collect(Collectors.toList()));

But for example I would like to take into account only one or only two fields of this class. Of course I can add a lot of code and finally solve my problem, but maybe I can do it in better way?
One idea I had: create some final FirstTypeMD finalConf; class object which will have only important fields and than try to compare objects:

ArrayList<FirstTypeMD> resArr = new ArrayList<FirstTypeMD>(resArr1.stream()
                            .filter(c -> c.electricProtect == finalConf.electricProtect).collect(Collectors.toList()));

but I received the error which said that I hadn't have some fields which are null like this:

Cannot invoke "testwizard.wizards.FirstTypeMD.setElectricProtect(java.lang.Boolean)" because "this.this$0.finalConf" is null  

I don't know maybe I would like something impossible but I decided to ask you :)

7
  • I am not clear as to what exactly you're trying to achieve by passing just the first constructor parameter, but you can use constructor overloading to do it. The other parameters can be defaulted to some default values. This is a built-in feature for languages like Kotlin and JS, but not in Java. Commented Nov 26, 2020 at 12:41
  • So you want the other attributes to just get "default" values like null or 0 without defining 6! different constructors? Besides just having a no-parameter constructor and then setting the single value, you could use a Builder pattern, e.g. FirstTypeMD.new().withEnergyCons(energyCons) Commented Nov 26, 2020 at 12:41
  • @DebanikDawn, I moved for some tasks from kotlin, so maybe I don't know some details ) can you clarify your comment please? Commented Nov 26, 2020 at 12:43
  • @tobias_k, if it possible yes, but also it will be good not to add def values Commented Nov 26, 2020 at 12:44
  • @tobias_k, but what if I would like to take into account several fields which will have to be removed in some condition Commented Nov 26, 2020 at 12:45

1 Answer 1

1
public class Main {
    FirstTypeMD firstTypeMD = new FirstTypeMD(1);
}

class FirstTypeMD extends BaseParams {
    Integer framePerSec;

    public FirstTypeMD(Integer energyCons, Integer mass, Integer tempRange, Boolean electricProtect,
                       Boolean radioProtect, Integer framePerSec) {
        super(energyCons, mass, tempRange, electricProtect, radioProtect);

        this.framePerSec = framePerSec;
    }

    public FirstTypeMD(Integer energyCons) {
       
    }

    public void setFramePerSec(Integer framePerSec) {
        this.framePerSec = framePerSec;
    }

    public Integer getFramePerSec() {
        return framePerSec;
    }
}

In here I used two constructors

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

3 Comments

This works for this specific case, but what if there should also be a way to create an object with just mass? You can't have two constructors with the same parameter types (one Integer), and even if you could, you'd need 64 constructors to cover all combinations.
Otherwise you better use setters 6 times
@tobias_k, you are right of course) I will need too many combinations what won't be soo good

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.