Variable | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Variable

how I can call variable from method to another class ?

10th Apr 2020, 12:29 PM
Reema
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.
10th Apr 2020, 12:50 PM
John Robotane
John Robotane - avatar
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()); } }
10th Apr 2020, 1:07 PM
Muhammad Bilal
Muhammad Bilal - avatar
0
thank you
10th Apr 2020, 1:26 PM
Reema