+ 1
Using variable
Hi, I want to use an x variable created from a "Pop" class. I want to use it on a "Pro" class. Please look at my sample code and see where I'm going wrong. https://code.sololearn.com/cAdZPdODeqGF/?ref=app https://code.sololearn.com/cAdZPdODeqGF/?ref=app
1 Réponse
+ 3
write a getter method: public int getX(){ return x;} in the class that contains x.
when you need to access it from another class use getX() function.
Edit: I've noticed x is a variable in pop that you want to use in Pro. in that case you need to have specify the object of pop whose x you want as x is an instance variable. If you want just one x for all objects of Pop use public static int x to declare x. then you can get x using Pop.getX();