java - How to refer to a variable with method scope from a different method called from method in which variable is declared? -


suppose if have code -

    public class rectangle {  int length, breadth; // used else different purpose/calculations public int area(int length, int breadth) { int result = length*breadth; resetvalues(); return result;  } public void resetvalues()  {  length = ""; breadth = ""; } 

so, in above code, since calling resetvalues method area, length , breadth variables defined in area method referred/changed in resetvalues method rather ones declared in class level.

how achieve this? how can make resetvalues method refer variables declared in area method when called method?

the variables in area() local or method scoped. cannot accessed outside method. understanding of variable scopes

you can reset variable inside area method directly , not affect class variables:

public int area(int length, int breadth) { int result = length*breadth; length = 0; breadth = 0; return result;  } 

here reference gives clear descriptions:http://www.java-made-easy.com/variable-scope.html


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -