0

I'm new to learning Java and was trying to understand OOP, but I can't seem to find anyone who has the same exact question. My question is, is it okay to use methods inside a constructor like the example here:

package ezrab.nl;

public class Calculations {

    private int number;
    private int multiplier;

    private String operator = "";

    public Calculations(int number, String operator, int multiplier) {
        this.number = number;
        this.operator = operator;
        this.multiplier = multiplier;
        switch (getOperator()) {
        case "+":
            System.out.println(getNumber() + getMultiplier());
            break;
        case "-":
            System.out.println(getNumber() - getMultiplier());
            break;
        case "*":
            System.out.println(getNumber() * getMultiplier());
            break;
        case "/":
            System.out.println(getNumber() / getMultiplier());
            break;
        case "%":
            System.out.println(getNumber() % getMultiplier());
            break;
        default:
            System.out.println("Something went wrong.");
        }
    }

    public int getNumber() {
        return this.number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getMultiplier() {
        return this.multiplier;
    }

    public void setMultiplier(int multiplier) {
        this.multiplier = multiplier;
    }

    public String getOperator() {
        return this.operator;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }

}

So I'd like to know, is it allowed to use the methods I've created inside the constructor.

EDIT: I'd like to point out that the program is working. I just want to know if I followed the rules to OOP correctly.

11
  • Are you getting any error? Commented Jul 30, 2018 at 11:13
  • nope, not really, unless you have to. One thing here, is that methods used inside a constructor must not be polymorphic, make these final Commented Jul 30, 2018 at 11:13
  • I don't see anything wrong with what you have done, your code should compile and run at least. More typically, in production, you might see something doing logging statements, instead of making calls to System.out. Commented Jul 30, 2018 at 11:13
  • @Eugene I don't think he actually needs to call these methods, since they're just getters and the values are already available as parameters to the constructor. So, the only issue then, would be if println is final/static. Commented Jul 30, 2018 at 11:15
  • Imho, I advise you to create a separate method print. Because constructor must only create and initialize the object. And not to print or do something. For each action create a method. Commented Jul 30, 2018 at 11:16

4 Answers 4

4

Put behavior of object separately with creation:

public class Calculations {

private int number;
private int multiplier;
private String operator = "";

public Calculations(int number, String operator, int multiplier) {
    this.number = number;
    this.operator = operator;
    this.multiplier = multiplier;
}

public int getNumber() {
    return this.number;
}

public void setNumber(int number) {
    this.number = number;
}

public int getMultiplier() {
    return this.multiplier;
}

public void setMultiplier(int multiplier) {
    this.multiplier = multiplier;
}

public String getOperator() {
    return this.operator;
}

public void setOperator(String operator) {
    this.operator = operator;
}

public void print() {
     switch (getOperator()) {
    case "+":
        System.out.println(getNumber() + getMultiplier());
        break;
    case "-":
        System.out.println(getNumber() - getMultiplier());
        break;
    case "*":
        System.out.println(getNumber() * getMultiplier());
        break;
    case "/":
        System.out.println(getNumber() / getMultiplier());
        break;
    case "%":
        System.out.println(getNumber() % getMultiplier());
        break;
    default:
        System.out.println("Something went wrong.");
    }
}
}
Sign up to request clarification or add additional context in comments.

Comments

1

is it allowed to use the methods I've created inside the constructor.

It's allowed but dangerous as you need to know if everything which should be set, has been set. It's better to use the value which was passed as a parameter.

However your switch should be in the constructor as you can change the operator or operand later. I would have a separate method for it.

NOTE: Having a field called multiplier is confusing as it's not a multiplier in most cases.

Comments

1

Yes it's allowed but better way is to separate the behavior and construction of object. Constructors are mainly used to set properties of the class.

Comments

0

To better code in OOP (Object Oriented Programming) this concept you are referring to is known as Encapsulation. You can generally code in the way you are saying to do it. However, down the line when the application gets bigger, it would make your life much easier if you follow established design patterns. Please review this link for the Encapsulation concept: https://www.tutorialspoint.com/java/java_encapsulation.htm

Comments

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.