Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    37
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Decrement A variable from another class

    How would I grab variable from another class and add decrementer.
    I tried this but it didnt work.
    int quarters1=CasinoGame.quarters;
    quarters1--;

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Not enough information.
    Is this within a scope to be decremented? Also, int is a primitive value, and as such quarters1 may equate to CasinoGame.quarters (assuming a static int) initially, but they are not the same variable. Decrementing quarters1 will have no effect on CasinoGame.quarters. You need to use an Integer if that is the behaviour you want.

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    37
    Thanks
    1
    Thanked 0 Times in 0 Posts
    How would I make it so that the variable is shared with all classes I create in one java project. I'm trying to decrement the same variable from different classes.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by anonymous9 View Post
    How would I make it so that the variable is shared with all classes I create in one java project. I'm trying to decrement the same variable from different classes.
    If its publicly accessible member, you can decrement it wherever you would like by direct access. If its retrieved, you need to treat it as an object by using the Integer class.

  • #5
    New Coder
    Join Date
    Jan 2012
    Posts
    37
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok I got that working but when I try to pass the CasinoGame.quarters into method inside SlotMachine Class I get an error. Can anyone rewrite this correctly so I can see how it is done.
    Heres both classes

    Code:
    import java.util.Scanner;
    
    public class CasinoGame {
    
    	public static int quarters;
    
    	public static void main(String[] args) {
    		int quarters;
    
    		System.out.println("Please enter how many quarters are in the jar: ");
    		Scanner keyboard = new Scanner(System.in);
    		quarters = keyboard.nextInt();
    
    	}
    }
    Code:
    public class SlotMachine {
    
    	public static void main(String[] args) {
    		
    public void Slot1(int Casino.quarters){
    CasinoGame.quarters--;
    
    }
    }
    }public class SlotMachine {
    
    	public static void main(String[] args) {
    		
    public void Slot1(int Casino.quarters){
    CasinoGame.quarters--;
    
    }
    }
    }

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You cannot do that as Casino.quarters is a value, not a definable variable as a parameter.
    Slot1 should take no parameters.


  •  

    Posting Permissions

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