Jump to content

Can't switch boolean from another class


Recommended Posts

Posted

I can't switch any booleans from another class.

public class Main {
public boolean hans = false;

public int onLoop() {
	Processing p = new Processing();
	p.exchangeContext(getBot();
	log("Hans is" + hans);
	p.Loop();
	return 500;
	}

}

public class Processing {
	public void Loop() {
	Main m = new Main();
	m.exchangeContext(getBot());
	log("Switching hans to true)"
	m.hans = true;
	}


}

As soon as I use this loop, the log will say:

[INFO][Bot #1][04/03 04:14:29 PM]: Switching hans to true
[INFO][Bot #1][04/03 04:14:29 PM]: hans is false

Pretty sure it's some dumb mistake but I can't get it.

 

  • Like 1
Posted (edited)
13 minutes ago, The Undefeated said:

I can't switch any booleans from another class.


public class Main {
public boolean hans = false;

public int onLoop() {
	Processing p = new Processing();
	p.exchangeContext(getBot();
	log("Hans is" + hans);
	p.Loop();
	return 500;
	}

}

public class Processing {
	public void Loop() {
	Main m = new Main();
	m.exchangeContext(getBot());
	log("Switching hans to true)"
	m.hans = true;
	}


}

As soon as I use this loop, the log will say:

[INFO][Bot #1][04/03 04:14:29 PM]: Switching hans to true
[INFO][Bot #1][04/03 04:14:29 PM]: hans is false

Pretty sure it's some dumb mistake but I can't get it.

 

Make hans static (However I'm sure that's a bad idea unless they're final) or in your main class have a method that's responsible for updating the value?

I.e:

public class Main {
public boolean hans = false;

public void setHansValue(boolean val) {
	this.hans = val;
}

public int onLoop() {
	Processing p = new Processing();
	p.exchangeContext(getBot();
	log("Hans is" + hans);
	p.Loop();
	return 500;
	}

}

public class Processing {
	public void Loop() {
	m.exchangeContext(getBot());
	log("Switching hans to true)"
	Main.setHansValue(true);
	}
}

 

Edited by harrypotter
  • Like 1
Posted (edited)
1 hour ago, The Undefeated said:

I can't switch any booleans from another class.


public class Main {
public boolean hans = false;

public int onLoop() {
	Processing p = new Processing();
	p.exchangeContext(getBot();
	log("Hans is" + hans);
	p.Loop();
	return 500;
	}

}

public class Processing {
	public void Loop() {
	Main m = new Main();
	m.exchangeContext(getBot());
	log("Switching hans to true)"
	m.hans = true;
	}


}

As soon as I use this loop, the log will say:

[INFO][Bot #1][04/03 04:14:29 PM]: Switching hans to true
[INFO][Bot #1][04/03 04:14:29 PM]: hans is false

Pretty sure it's some dumb mistake but I can't get it.

 

You are changing the boolean value in one instance of the class and outputting the boolean value from another. They aren't the same value.

The solution is to use getters and setters to change fields within a class.

Edited by Final
  • Like 2
Posted
8 minutes ago, Final said:

You are changing the boolean value in one class and outputting the boolean value from another. They aren't the same value.

 

11 minutes ago, harrypotter said:

Make hans static (However I'm sure that's a bad idea unless they're final) or in your main class have a method that's responsible for updating the value?

I.e:


public class Main {
public boolean hans = false;

public void setHansValue(boolean val) {
	this.hans = val;
}

public int onLoop() {
	Processing p = new Processing();
	p.exchangeContext(getBot();
	log("Hans is" + hans);
	p.Loop();
	return 500;
	}

}

public class Processing {
	public void Loop() {
	m.exchangeContext(getBot());
	log("Switching hans to true)"
	Main.setHansValue(true);
	}
}

 

Some Java basics I completely forget sometimes, I am ashamed.

Thanks both.

 

Posted (edited)
14 minutes ago, Final said:

You are changing the boolean value in one instance of the class and outputting the boolean value from another. They aren't the same value.

The solution is to use getters and setters to change fields within a class.

 

17 minutes ago, harrypotter said:

Make hans static (However I'm sure that's a bad idea unless they're final) or in your main class have a method that's responsible for updating the value?

I.e:


public class Main {
public boolean hans = false;

public void setHansValue(boolean val) {
	this.hans = val;
}

public int onLoop() {
	Processing p = new Processing();
	p.exchangeContext(getBot();
	log("Hans is" + hans);
	p.Loop();
	return 500;
	}

}

public class Processing {
	public void Loop() {
	m.exchangeContext(getBot());
	log("Switching hans to true)"
	Main.setHansValue(true);
	}
}

 

http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value

http://softwareengineering.stackexchange.com/questions/21802/when-are-getters-and-setters-justified

http://stackoverflow.com/questions/1568091/why-use-getters-and-setters

http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil

Read pls :gnome:

Edited by Vilius

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...