Jump to content

Can't switch boolean from another class


The Undefeated

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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