0

How can i access variable from one method in another method from the same program?

18th Dec 2018, 12:59 PM
Atharv Kulkarni
Atharv Kulkarni - avatar
5 Answers
18th Dec 2018, 3:21 PM
StealthyTaco
StealthyTaco - avatar
+ 4
That involves the concept of closure In short, you need to either Pass your variable as argument into the function being called Or Declare the variable in global scope For further reading, https://data-flair.training/blogs/java-closure/
18th Dec 2018, 1:13 PM
Gordon
Gordon - avatar
+ 3
public class Program{ int i = 0; public Program sum(int x, int y){ i=x+y; return this;} void getValue(int multi){ int total = i*multi; System.out.print(total);} public static void main(String[] args) { Program obj = new Program(); obj.sum(5,5).getValue(2);} }
18th Dec 2018, 1:31 PM
D_Stark
D_Stark - avatar
+ 3
I think people are over complicating this for you, have you heard of the "return" statement?
18th Dec 2018, 1:58 PM
StealthyTaco
StealthyTaco - avatar
+ 1
No i guess... Will you help me
18th Dec 2018, 1:59 PM
Atharv Kulkarni
Atharv Kulkarni - avatar