0

Which is a better way to do this, in terms of performance or other factors?

Say I am creating a class with jut one method in it that takes 2 values and adds them together.

Is it better to have 2 instance variables to store the 2 values then have a method which uses these, or is it better to just pass the variables in as parameters when calling the method?

Note: this class is only ever created just to use this method.

public class MyClass {
    int value1;
    int value2;

public MyClass(int value1, int value2){
    this.value1 = value1;
    this.value2 = value2;
}

public int MyMethod(){
    return value1 + value2;
}

or this way:

public class MyClass {

public MyClass(){

}

public int MyMethod(int value1, int value2){
    return value1 + value2;
}

Or would it be better to just use a static method, so I don't need to create an object every time I use the method?

4
  • first of: Java doesn't have global variables. secondly: why would you have a class like that? usually, you'll have more methods that want to use those values. if you only have them as params in a method, and don't store them anyware, how would you do that? Commented Jun 5, 2017 at 12:02
  • It depends 100% on your use case. Commented Jun 5, 2017 at 12:05
  • Not a global, instance/ member vvariables Commented Jun 5, 2017 at 12:05
  • @Stultuske sorry for using the wrong terms, changed them now. And for example I have a very specific task that needs done and doesn't fit in any of my other classes Commented Jun 5, 2017 at 12:20

3 Answers 3

1

Which is a better way to do this, in terms of performance or other factors?

These ways have no relation with performance. These are way of designing.

The first form (storing the arguments as fields):

public MyClass(int value1, int value2){
    this.value1 = value1;
    this.value2 = value2;
}

public int myMethod(){
    return value1 + value2;
}

makes sense if you want to save the passed information in the class in order to be able to invoke myMethod() when you want (the creation of the object and the computation may be differed in the time) but also to use value1 and value2 later in another methods of the instance.

The second form (using only the arguments) makes sense if you want only compute the result :

public int myMethod(int value1, int value2){
    return value1 + value2;
}

Use one or the other one according to your need.

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

Comments

1

It depends very match on your intention.

  1. If you name you value1 and value2 as engineWeight and wheelsWeight then you can name your method:

    public int calculateWeightSum(){ return value1 + value2; }

In such case it will have sense to have such kind of approach.

  1. The second approach(with method params) may be used as usual business logic of the object. You give variable to operate and it returns result. The result may depend on which object you make an invocation. That's the reason you don't mark this method as static. If you mark it as static, the object state won't affect the result.

  2. Java doesn't have "global" variables. It has static public variables that can be treated very similarly. You can use them with both approaches, but the intention of using it here is very unclear. Probably in this case is better to make utility with static methods to do the job.

As for performance, it's pretty much the same in all the cases. But, in the approach with constructor you use additional memory to store field for each object. But, again. It depends very much on your intention, here it's difficult to judge until you provide more information about real task. For now I just described my ideas on how it's convenient to use different approaches.

Comments

1

In Java, variables that are in class called member or instance variables not global.

public class MyClass {
    int value1;
    int value2;

These two variables need to use base upon your reqiurements. First of all implement make and structure, think about what do you want to achieve. Then decide what classes and what members are needed for these classes(methods/ variables).

As a example, these two int value1; int value2; variable may be needed when you want to perform another tasks using same values. Such as divide(), multiply(), etc.

public class MyClass {
    int value1;
    int value2;

    public MyClass(int value1, int value2){
        this.value1 = value1;
        this.value2 = value2;
    }

    public int add(){
        return value1 + value2;
    }

    public int sub(){
        return value1 - value2;
    }

    public int divide(){
        return value1 / value2;
    }

    public int multiply(){
        return value1 * value2;
    }
}

Using like public int MyMethod(int value1, int value2){ method you only can perform task. You won't be able to save variables values to perform another task. Like that methods helpful to divide tasks.

public class MyClass {
    public int add(int value1, int value2){
        return value1 + value2;
    }

    public int sub(int value1, int value2){
        return value1 - value2;
    }

    public int divide(int value1, int value2){
        return value1 / value2;
    }

    public int multiply(int value1, int value2){
        return value1 * value2;
    }
}

But you can take two arguments each for methods perform task return value, that's all(all are different, donot have anything related).

Summary:

Both have unique functionality and roll to play according to your requirements.

Clean code is good book to read. And read these javanote, q1 and q2

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.