Results 1 to 9 of 9
- 10-24-2011, 06:56 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
How to change variables from a method in a different class
Hi, Im making a game with bouncing balls, and I want a method inside a separate class to tell the main method the coordinates where to draw the balls on.
The thing is, im having some problems with finding a way to modify these values (x and y) from the different class and make them take effect in the main one.
So far I know only one way, which is to make methods inside the main class that modify the variables and call them from the accessory class, but it seems cumbersome and makes code harder to read.
Is there a better way to do this?
Java Code://for example: public class balls(){ public static void main(String[] args){ class 2 behave = new class2(); int x = 2; int y = 2; for (int i = 0; i < 90;){ behave.newXandY(); drawtheball(x, y); //Ill avoid writing all the code for drawing it here } } } //this is not an inner class! public class class2{ public void newXandY(){ x = x++;// here is the problem: this values of x and y wont affect the original values in the other class i want to use! y = y++; } }Last edited by tomasmk; 10-24-2011 at 07:42 PM.
-
Re: How to change variables from a method in a different class
- I wouldn't use inner classes as you're doing. Rather each java file should be in its own class at this stage in your education.
- You'll need public getter and setter methods if you want to alter the state of a class.
- 10-24-2011, 07:21 PM #3
Member
- Join Date
- Jul 2011
- Location
- New Delhi,India
- Posts
- 56
- Rep Power
- 0
Re: How to change variables from a method in a different class
i am just a newbie....but i think you should make the variables as public in the class balls
- 10-24-2011, 07:47 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
Re: How to change variables from a method in a different class
the class2 is not meant to be inner, its meant to be in its own file.
so your saying that the only option would be to invoke a method from class2, like this:
Java Code:public class balls(){ public static void main(String[] args){ int x = 2; int y = 2; balls myballs = new balls(); class 2 behave = new class2(); for (int i = 0; i < 90;){ behave.newXandY(); drawtheball(x, y); //Ill avoid writing all the code for drawing it here } } public void raiseX(){ x = x++; } public void raiseY(){ y = y++; } } //this is not an inner class! public class class2{ public void newXandY(){ myballs.raiseX(); myballs.raiseY(); } }
this means that I must always produce all the changes i want in my class from methods inside that very class, and I can only "activate" them from an outer class, which is very inconvenient. At that point i could just go procedural and write all the instructions within that class, but im trying to learn OO here :/
- 10-24-2011, 07:59 PM #5
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: How to change variables from a method in a different class
This doesn't do what you intend it to. If you want to increment x, just use x++ on its own. As it stands, x++ increments x but evaluates to the current value of x, which it then assigns to x and overwrites the incremented value.Java Code:public void raiseX(){ x = x++; } public void raiseY(){ y = y++; }
- 10-24-2011, 09:20 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
Re: How to change variables from a method in a different class
k iron, thanks, do you know of any way to use x++ from outside the class that contains x?
- 10-24-2011, 09:54 PM #7
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Re: How to change variables from a method in a different class
Make a method like so:
And then call it from a different class:Java Code:public void incrementX() { x++; }
(if it's a static, class variable)
This is incrementing x for the class.Java Code:ClassName.incrementX();
(if it's a local variable, like for an object)
This is incrementing x for the individual "thing" object.Java Code:ObjectName thing = new ObjectName(); thing.incrementX();
- 10-24-2011, 10:28 PM #8
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
Re: How to change variables from a method in a different class
thanks Solar, i already knew that possibility, I made the thread to ask if there was some other more convenient way to do it, without having to fill the class with methods that target their own class
- 10-24-2011, 10:34 PM #9
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Similar Threads
-
Can not change variables.
By Pojahn_M in forum New To JavaReplies: 7Last Post: 09-09-2011, 09:25 AM -
can change private member of class from other class
By besil in forum New To JavaReplies: 2Last Post: 02-16-2011, 11:35 AM -
accessing variables in a method that returns a void
By mochajava in forum New To JavaReplies: 2Last Post: 02-12-2011, 08:00 PM -
getting class to recognize variables from another class
By shadycharacter in forum New To JavaReplies: 1Last Post: 04-26-2010, 10:14 PM -
Exception Class for class that compares objects variables. Help Please.
By darkblue24 in forum New To JavaReplies: 1Last Post: 01-03-2010, 09:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks