Results 1 to 5 of 5

Thread: Boolean help

  1. #1
    Join Date
    Dec 2011
    Location
    East Coast, USA
    Posts
    4,231
    Mentioned
    112 Post(s)
    Quoted
    1869 Post(s)

    Default Boolean help

    I have a class in which I have declared a boolean value, like so.

    Class1:
    Code:
    public static boolean GLOBAL_PVP = false;
    And in this class, I have created a method to return the boolean. I have done this so that I can import Class1 into Class2, and have Class2 read the boolean's value.

    Class1:
    Code:
    public static boolean GLOBAL_PVP = false;
    
    public static boolean GlobalPvp () {return GLOBAL_PVP;}
    I read some material on stackoverflow that seemed to suggest this was a poor way of handling booleans, but I don't know how else to do it.

    I can then access the boolean GLOBAL_PVP from Class2 like so:

    Class2:
    Code:
    if (Class1.GlobalPvp()) {
        return true;
    }
    (this is inside yet another boolean that determines if combat is possible or not. I wish to be able to override this check if global PvP is enabled, and thus, GLOBAL_PVP is true.)

    ***

    Here's where I'm stuck. I can get the value just fine. But how do I set the value of GLOBAL_PVP from Class2?

    Thanks for any light you can shed, I'm very new at Java.
    Last edited by KeepBotting; 04-23-2015 at 01:11 AM.
    GitLab projects | Simba 1.4 | Find me on IRC or Discord | ScapeRune scripts | Come play bot ScapeRune!

    <BenLand100> we're just in the transitional phase where society reclassifies guns as Badâ„¢ before everyone gets laser pistols

  2. #2
    Join Date
    May 2014
    Posts
    4
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    It really just depends on what you're trying to do.

    Since your variable is public, you don't even need get/set methods. You can just do

    Code:
    Class1.GLOBAL_PVP = true;
    To set it, and you can just access it the same way, i.e.:

    Code:
    if(Class1.GLOBAL_PVP)
    But like Stack Overflow, this is sort of a shitty way of doing things.

  3. #3
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    why would u need to make it global?
    make the method in Class2 accept a Class1 object, then you can use getter/setter methods of Class1 on the object. Or inherit Class1 if appropriate, not sure whats the exact situation.

  4. #4
    Join Date
    Dec 2007
    Posts
    289
    Mentioned
    4 Post(s)
    Quoted
    86 Post(s)

    Default

    I would recommend using getters and setters.

    Example below.

    Code:
    public class A {
       
        private static boolean exampleVar = false;
    
        public static boolean getExampleVar() {
            return exampleVar;
        }
    
        public static void setExampleVar(boolean exampleVar) {
            A.exampleVar = exampleVar;
        }
    
    }
    
    
    
    public class B {
       
        public static void main(String[] args) {
            System.out.println(A.getExampleVar()); //prints out false
            A.setExampleVar(true);
            System.out.println(A.getExampleVar()); //prints out true
        }
    
    }

  5. #5
    Join Date
    Dec 2011
    Location
    East Coast, USA
    Posts
    4,231
    Mentioned
    112 Post(s)
    Quoted
    1869 Post(s)

    Default

    Quote Originally Posted by riwu View Post
    why would u need to make it global?
    make the method in Class2 accept a Class1 object, then you can use getter/setter methods of Class1 on the object. Or inherit Class1 if appropriate, not sure whats the exact situation.
    Quote Originally Posted by honeyhoney View Post
    I would recommend using getters and setters.

    Example below.

    Code:
    public class A {
       
        private static boolean exampleVar = false;
    
        public static boolean getExampleVar() {
            return exampleVar;
        }
    
        public static void setExampleVar(boolean exampleVar) {
            A.exampleVar = exampleVar;
        }
    
    }
    
    
    
    public class B {
       
        public static void main(String[] args) {
            System.out.println(A.getExampleVar()); //prints out false
            A.setExampleVar(true);
            System.out.println(A.getExampleVar()); //prints out true
        }
    
    }
    Oh, thanks guys. Kinda forgot about this thread, I never subscribed to it :fp:

    hadesflames walked me through the logic of his example through Skype, I've since then set up many booleans that can be toggled cross-class.

    I will probably revamp them in the future to use getter and setter methods, as I see those everywhere and they seem like a very elegant way to do things.

    Thanks again!
    GitLab projects | Simba 1.4 | Find me on IRC or Discord | ScapeRune scripts | Come play bot ScapeRune!

    <BenLand100> we're just in the transitional phase where society reclassifies guns as Badâ„¢ before everyone gets laser pistols

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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