Results 1 to 14 of 14
  1. #1
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default change variable value from another class?

    Ok I woking on a text based game and now I need some help.
    If I have two classes and I have a variable in the first class how can I then change the value of this variable from the class 2nd ?
    example:
    class 1:
    Java Code:
    public class 1{
          public static void main(String[] args){
          //The value of this variable is 10 right now but I want it to be 20 how can I do this from class 2 ?     
          int Changeme = 10;
          }
    }
    
    class 2:
    
    public class 2{
          public static void main(String[] args){   
          //I want to change the value of changeme from this class how can I do that ?
          }
    }
    If anyone know how I can do this it will really helpful :).
    Last edited by Norm; 01-02-2012 at 03:43 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: change variable value from another class?

    how can I then change the value of this variable from the class 2nd
    You can not access variables that are local to a method from another class.
    If the variable were a class variable, then you could have a method in Class1 that changes the value of the variable and then from Class2 you could use a reference to Class1 to call that method.

    In Class1:
    int changeMe; // defined as a class variable

    public void setValue(int theVal) {
    changeMe = theVal; // change the value
    }

    In Class2:

    refToClass1.setValue(theValueToBeUsed);

  3. #3
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: change variable value from another class?

    What if its a boolean I wanna change from false to true ?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: change variable value from another class?

    Then pass a boolean value to the set method instead of an int.

  5. #5
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: change variable value from another class?

    It dosn't seems to work whats wrong with this code ?
    code:
    refToItem.setValue(true);

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: change variable value from another class?

    It dosn't seems to work
    Please explain.
    whats wrong with this code ?
    Hard to tell. You have only shown 1/2 of what is going on.
    What is the definition for the setValue() method? What type of argument does it take?

  7. #7
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: change variable value from another class?

    Im making a text based game and I want to make this class for weapons and other things I call the class "Items".
    When I buy A weapon in a shop I want the main class to change the boolean for the weapon from false to true so the programme know that I have the weapon.
    Here is the Item class:
    public class Items {
    public void SPItem(boolean FishingRod) {
    FishingRod = false;
    }
    public void BDItem(boolean BronzeDagger){
    BronzeDagger = false;
    }
    public void BSItem(boolean BronzeSword){
    BronzeSword = false;
    }
    public void BSHItem(boolean BronzeShield){
    BronzeShield = false;
    }
    public void IWItems(boolean IronDagger){
    IronDagger = false;
    }
    public void ISItem(boolean IronSword){
    IronSword = false;
    }
    public void ISHItem(boolean IronShield){
    IronShield = false;
    }
    }

    and in the main class I have this code:

    import java.util.Scanner;

    public class HeroBridge{
    public static void main(String[] args){
    Scanner Sname = new Scanner(System.in);
    Sleeper sp = new Sleeper();

    System.out.println("Hello whats your name?");
    System.out.println("Name:");
    String ym = Sname.nextLine();
    System.out.println("Hello"+ym+" And welcome to HeroBridge");
    System.out.println("Befor you start I'll give you a sword and som gold coins");
    sp.sleep5();
    System.out.println("*You have just got a small sword and 100 gold coins*");
    int Money = 0;
    Money = Money + 100;
    Items It = new Items();
    ItrefToItems.setValue (true);
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: change variable value from another class?

    Java Code:
    public void SPItem(boolean fishingRod) {
      fishingRod = false;
    }
    This method and all others just like it are giving a value to a local variable that disappears when the method exits. fishingRod is defined locally to the SPItem method.

    What are you trying to do with these methods? The class has no variables that you can give values to.
    Where is the setValue() method defined?
    Where is the ItrefToItems variable defined?

  9. #9
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: change variable value from another class?

    each method should be a item :).
    The setValue(); method is in the main class :).
    How can I do the variables useable ?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: change variable value from another class?

    each method should be a item :).
    Please explain.
    methods belong to a class. They are not things like a class object.

    How can I do the variables useable ?
    Please explain.

    The setValue(); method is in the main class :).
    What is the name of the main class? Where is its definition?
    You posted code for two classes: Items and HeroBridge

  11. #11
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: change variable value from another class?

    1. As you know its for a text based game each metod should be a Item or a weapon :).
    2. That with to make the variables useable I refers to that you wrote: This method and all others just like it are giving a value to a local variable that disappears when the method exits. fishingRod is defined locally to the SPItem method.
    3. The Herobrige class is the main class and the Items class is a kind of sub-class I think ?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: change variable value from another class?

    What compiler errors are you getting now? Please copy and paste them here.

    When you talk about classes, use the name of the class. I have no idea which class you think is the main one or a sub class.

  13. #13
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: change variable value from another class?

    The main class is Herobrige and the error is that it says that it can't make the method setValue(boolean);.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: change variable value from another class?

    Please post the full text of the error message.
    Where is the setValue method defined? Post the code for the class where the method is defined.

Similar Threads

  1. change variable given as argument to function
    By Danieljabailey in forum New To Java
    Replies: 4
    Last Post: 07-27-2011, 11:48 PM
  2. Change a variable outside TimerTask after it is done?
    By a_programmer in forum Advanced Java
    Replies: 3
    Last Post: 07-20-2011, 03:04 AM
  3. Replies: 1
    Last Post: 04-17-2011, 03:59 PM
  4. Why wont my boolean variable change?
    By equal in forum New To Java
    Replies: 8
    Last Post: 02-22-2011, 10:43 PM
  5. can i listen to variable change in diffrent class
    By gloomy1991 in forum New To Java
    Replies: 2
    Last Post: 01-08-2011, 01:50 AM

Posting Permissions

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