SRL Archive - Java Help and Tutorials

Boolean help


KeepBotting

2015-04-22 21:07:50

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

Class1:

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:

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:

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.

hadesflames

2015-04-22 21:14:13

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

Class1.GLOBAL_PVP = true;

To set it, and you can just access it the same way, i.e.:

if(Class1.GLOBAL_PVP)

But like Stack Overflow, this is sort of a shitty way of doing things.

riwu

2015-04-22 22:24:52

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.

honeyhoney

2015-05-14 13:30:47

I would recommend using getters and setters.

Example below.

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
}

}

KeepBotting

2015-05-14 17:45:34

From: riwu
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.


From: honeyhoney
I would recommend using getters and setters.

Example below.

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!