Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Hisham Younis   on Feb 08 In Java Category.

  
Question Answered By: Abana Cohen   on Feb 08

Variables which are declared within a method  have
a scope local to that method.<br><br>If you want
variables accessable from method to method, you need to
declare them in the class.<br><br>For
example:<br><br>public class  myClass{<br>private int
firstVariable;<br><br><br>public method1(){<br> firstVariable = 10;<br>
System.out.println("FirstVariable: =" + firstVariable);<br>}<br><br>public
method2(){<br> int localVariable = firstVariable;<br>
firstVariable = firstVariable + localVariable;<br>
System.out.println("FirstVariable has Changed: + firstVariable);<br>
System.out.println("LocalVariable now equals : " +
localVariable);<br>}<br><br>}<br><br>While one can do this, it is not a very
good
programming practice. variables  which you want to share
access to should be passed into a method and returned if
they need to be changed outside the scope of that
method.<br><br>If you are changing too many "instance" variables,
it's time to look at your design. Doing this causes A
LOT of problems in maintance.

Share: 

 

This Question has 9 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on variable accesibility Or get search suggestion and latest updates.


Tagged: