Results 1 to 9 of 9
  1. #1
    tomasmk is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default 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

  4. #4
    tomasmk is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default 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 :/

  5. #5
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: How to change variables from a method in a different class

    Java Code:
    public void raiseX(){
    x = x++;
    }
    public void raiseY(){
    y = y++;
    }
    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.

  6. #6
    tomasmk is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default 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?

  7. #7
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: How to change variables from a method in a different class

    Quote Originally Posted by tomasmk View Post
    k iron, thanks, do you know of any way to use x++ from outside the class that contains x?
    Make a method like so:

    Java Code:
    public void incrementX() {
    x++;
    }
    And then call it from a different class:

    (if it's a static, class variable)
    Java Code:
    ClassName.incrementX();
    This is incrementing x for the class.

    (if it's a local variable, like for an object)
    Java Code:
    ObjectName thing = new ObjectName();
    thing.incrementX();
    This is incrementing x for the individual "thing" object.

  8. #8
    tomasmk is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default 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

  9. #9
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: How to change variables from a method in a different class

    Quote Originally Posted by tomasmk View Post
    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
    How I said would be the best way to do it.

Similar Threads

  1. Can not change variables.
    By Pojahn_M in forum New To Java
    Replies: 7
    Last Post: 09-09-2011, 09:25 AM
  2. Replies: 2
    Last Post: 02-16-2011, 11:35 AM
  3. accessing variables in a method that returns a void
    By mochajava in forum New To Java
    Replies: 2
    Last Post: 02-12-2011, 08:00 PM
  4. getting class to recognize variables from another class
    By shadycharacter in forum New To Java
    Replies: 1
    Last Post: 04-26-2010, 10:14 PM
  5. Replies: 1
    Last Post: 01-03-2010, 09:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •