How to get a value which declared and initialized in another method without using parameter in java?
public class Value {
void method1()
{
int demo=10;
System.out.println("methd 1"+demo);
}
void method2()
{
System.out.println("method 2"+demo);
}
public static void main(String[] args) {
Value obj = new Value ();
obj.method1();
obj,method2();
}
}
Here the variable demo is declared in method1 and assigned a value now I need to get the value of demo in method 2 is this possible without any parameter, global declaration, and no getter setter method?
method2()has no value fordemo, as it isn't defined. Inmain(), you haveobj,method2(), which should beobj.method2()