Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Update Values Globally

  1. #1
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post

    Question Update Values Globally

    Is there any way to update values globally? More specifically, boolean value. I'm trying to make a subroutine that "toggles" the given value, but it only does it locally. I've also tried hashmaps, but that's local also. Here' the test code I came up with:
    import java.util.Map;
    import java.util.HashMap;
    public class booltest {
    	private static boolean isFalse = false;
    	public static boolean isTrue = true;
        public static Map<String, Boolean> settings = new HashMap();
    	public static void main (String args[]){
    		settings.put("Test", false);
    		System.out.println(String.valueOf(settings.get("Test")));
    		toggleOnOff("Test");
    		System.out.println(settings.get("Test"));
    		toggleOnOff("Test");
    		System.out.println(String.valueOf(settings.get("Test")));
    		hashmaptest();
    	}
    	private static void toggleOnOff(String Value){
    		if (settings.get(Value).equals(isFalse))
    			settings.put(Value, true);
    		if (settings.get(Value).equals(isTrue))
    			settings.put(Value, false);
    	}
    	private static void hashmaptest(){
    		Map<String, Boolean> test =new HashMap();
    		test.put("test", false);
    		System.out.println(String.valueOf(test.get("test")));
    		test.put("test", true);
    		System.out.println(String.valueOf(test.get("test")));
    		test.put("test", false);
    		System.out.println(String.valueOf(test.get("test")));
    	}
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Update Values Globally

    Can you explain your problem in more details, especially what you mean by globally and locally. Technically there is no such thing as global in Java.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Update Values Globally

    I think I know what your problem is: the toggleOnOff method.

    Initially the boolean value of the "Test" key is false. In the method the first if statment is true so it resets the value to true. It then looks at the second if statement. Since this is now true (die to the above change) it enters the second if statement as well and resets the value back to false.

  4. The Following 2 Users Say Thank You to Junky For This Useful Post:

    JavaPF (June 10th, 2011), techwiz24 (June 10th, 2011)

  5. #4
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Update Values Globally

    You have no idea how much of an "oooooohhhhhhhh, DUH!" moment I had when I read that! Thanks, sometimes it just takes someone else to look at it, thanks a million, problem fixed!

Similar Threads

  1. button update when clicked
    By prettynew in forum AWT / Java Swing
    Replies: 4
    Last Post: March 13th, 2011, 04:47 PM
  2. Update table in database
    By CTheSky in forum JDBC & Databases
    Replies: 4
    Last Post: February 24th, 2011, 02:02 AM
  3. Cannot update Jlabel in JApplet
    By rin in forum Java Applets
    Replies: 2
    Last Post: April 17th, 2010, 08:21 AM
  4. Insert master or gloabal java script to disable globally onclick
    By karnesb in forum Java Theory & Questions
    Replies: 2
    Last Post: October 12th, 2008, 04:10 PM