Is there any way of accessing a variable that is in a public method of another class. I know that you can create an object of the other class and invoke methods from that clas but do not know if the value of a variable within a method can be 'accessed'. If you can could you please provide me an example.
Variables local to a method -- i.e., the variable "x":
can't be directly accessed outside of the method that declares them. They don't even exist when the method isn't being executed. But if you think you need to do this, it's just a design problem you can easily overcome. First, you may simply be able to change the method to return the value of interest.
If this isn't possible -- perhaps because the method already returns some other value -- then another option is to make the variable a member of the class, and provide an accessor for it: