0
Variable
how I can call variable from method to another class ?
3 Answers
0
what do you mean exactly?
call a given class method variable from another class?
I think it's not possible. but if the variable belongs to the class itself and is reachable(public or in some cases protected), you can access it with
ClassName.variableName
anyway, you can define a getter method for that variable.
0
//class in which we have our variable
class A{
private int a=2;
//setter need if you want to set another value of a
public void setter(int a){
this.a=a;
}
//method to get value of a
public int getter(){
return a;
}
}
//class to get value of a
class main{
//main method
public staic void main(String args[]){
//creating instance of class A
A obj=new A();
//calling getter method for printing value of a
System.out.println(obj.getter());
}
}
0
thank you