Using methods inside a constructor

default constructor in java
constructor program in java
what is the use of constructor
can we call a method inside a constructor in java
copy constructor in java
parameterized constructor in java
can we write a method inside a constructor in java
java constructor initialization

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.

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.");
    }
}
}

Constructors in Java, is known to have a negative impact on testability. Can't i just declare a method inside a constructore. – user2225994 May 8 '13 at 0:59 There's a naming conflict - nightsStay cannot be both a long and a void () . – michaelb958--Reinstate Monica May 8 '13 at 1:00

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.

Can I call methods in constructor in Java?, Can we call a method inside a constructor in C#? Prototype will enable us to easily define methods to all instances of the instances while saving memory. What's great is that the method will be applied to the prototype of the object, so it is only stored in the memory once, because objects coming from the same constructor point to one common prototype object.

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.

Constructor should generally not call methods, value 5; l = nightsStay(); // calling method nightsStay and storing the result in l; And if you want the method nightsStay() to return something then you must  A constructor in Java is a block of code similar to a method that’s called when an instance of an object is created. Here are the key differences between a constructor and a method: A constructor doesn’t have a return type. The name of the constructor must be the same as the name of the class. Unlike methods,

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

declaring method inside a constructor, In my opinion, we should try to only call either private or final methods from inside our constructors. The reason is that Java always calls the most  A constructor can invoke another constructor in the same object by using the this keyword. Like base , this can be used with or without parameters, and any parameters in the constructor are available as parameters to this , or as part of an expression.

[JavaSpecialists 210], Methods Within Constructor vs Prototype in Javascript. At many instances when working with javascript objects, different pieces of code can  Constructors are used to initialize the object’s state. Like methods, a constructor also contains collection of statements(i.e. instructions) that are executed at time of Object creation. Think of a Box. If we talk about a box class then it will have some class variables (say length, breadth, and height).

Methods Within Constructor vs Prototype in Javascript, How constructors are different from methods in Java? Constructor(s) must have the same name as the class within which it defined while it is not necessary for the method in java. Constructor(s) do not return any type while method(s) have the return type or void if does not return any value. A constructor is a method whose name is the same as the name of its type. Its method signature includes only the method name and its parameter list; it does not include a return type. The following example shows the constructor for a class named Person.

When to call the constructor and when to call the method in Java , If you don't need a new object, then don't use a constructor. Methods will be called directly by calling the default constructor when . not calculate anything and therefore it should not call any other method within the class. Calling virtual functions from a constructor or destructor is considered dangerous most of the times and must be avoided whenever possible. All the C++ implementations need to call the version of the function defined at the level of the hierarchy in the current constructor and not further. You can call a virtual function in a constructor. The

Comments
  • Are you getting any error?
  • nope, not really, unless you have to. One thing here, is that methods used inside a constructor must not be polymorphic, make these final
  • 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.
  • @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.
  • 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.