1

Is it a good practice to use properties as local variable. In cases where there are many methods which uses some variables, In each method the variable value changes. This avoids many times creating new variables and the code increases. Any suggestion?

private void method1(){
     int totalLength = length1 + 10;
     int totalBreath = (breath1 + breath2) + 20;
     int size =  (totalLength * totalLength);
     System.out.println(size);
}


private void method2(){
     int totalLength = length1 + 20;
     int totalBreath = (breath1 + breath2) + 30;
     int size =  (totalLength * totalLength);
     System.out.println(size);
}


private void method3(){
     int totalLength = length1 + 60;
     int totalBreath = (breath1 + breath2) + 10;
     int size =  (totalLength * totalLength);
     System.out.println(size);
}

As you can see, totalLength, totalBreath, size is repeated in every method. Can i make them as fields of the class? So, i need not declare it in every method.

 private void method1(){
     totalLength = length1 + 10;
     totalBreath = (breath1 + breath2) + 20;
     size =  (totalLength * totalLength);
     System.out.println(size);
}
FirmView
  • 3,130
  • 8
  • 34
  • 50

4 Answers4

2

I read your question as, "When should a local variable be promoted to be a field of the class?"

The best answer is "it depends" but then again, its accuracy is quickly eclipsed by its lack of usefullness.

Fields should have a relationship to the class itself, as in is the field an attribute of the class? I include an example below to illustrate the syntax difference, but I agree with this post that it should be avoided if it pollutes the meaning of the class.

Usually you only need to have a field when you need the value of the field to be maintained between calls to different methods for a given instance of the class, with the option of making it static when the value should be maintained between method calls for all instances of the class. It will depend on several factors like shop convensions, performance goals, existing codebase, etc. so there is no single right answer without specific code. This question seems to include similar points. If you find yourself using the approach below, you might consider other approaches like refactor the behavior into a help class.

Another question asks the same question but from the perspective of a programming student.

Examples:

public class VariableScope {
    int field1 = 3;

    void foo()  {
        int a = 2;

        // variable passing in width
        bar1(1);
        bar2(1);

        // variable passing in depth
        bar3(a);

        // uses a field to reduce variable passing
        baz1();
        baz2();

    }

    void bar1(int param)    {
        System.out.println("param=" + param);
    }

    void bar2(int param)    {
        System.out.println("param=" + param);
    }

    void bar3(int param)
    {
        System.out.println("Passing param to bar4");
        bar4(param);
    }

    void bar4(int param){
        System.out.println("param=" + param);
    }

    void baz1() {
        System.out.print("field1=" + field1);
    }

    void baz2() {
        System.out.print("field1=" + field1);
    }
}
Community
  • 1
  • 1
Kelly S. French
  • 12,198
  • 10
  • 63
  • 93
1

From what it sounds like, if you're using the variable for multiple methods you're should declare the variable as a global variable. But yes, If no other method needs that variable , and you don't want to be writing a bunch of return statements you can use local variables

user1577870
  • 126
  • 9
1

I suppose you mean a field by property which usually has accessor and mutator (get,set-methods).

In common you should keep the scope of a variable as small as possible. An example if you use many for loops like:

for ( int i = 0 ; i < 10 ; i++ ) {
}

and replace this by

int i;

method1() {
  for ( i = 0 ; i < 10 ; i++ ) {
     // some code;
  }
}

method2() {
  for ( i = 0 ; i < 10 ; i++ ) {
     // some code;
  }
}

If one thread calls method1() and another one method2() you would face a race condititon. You can easily introduce hard to find bugs in your code.

stacker
  • 68,052
  • 28
  • 140
  • 210
1

I assume you mean something like this:

Class foo() {
  int x;

public bar() {
  for(x = 0; x <100; ++x) ...
}  }

No, it's not good practice.

One place where it can even be harmful is in the synchronization/concurrency/multi-threaded case: if you are working with class members, they are going to need be synchronized which will eat into performance. Otherwise you risk multiple threads overwriting the value of the field and that can lead to errors in your program (likely hard to debug).

Colin D
  • 5,641
  • 1
  • 23
  • 35