This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes question about using boolean to check a condition of an object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question about using boolean to check a condition of an object?" Watch "question about using boolean to check a condition of an object?" New topic
Author

question about using boolean to check a condition of an object?

mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

hi

I have a program where i create an Object tv, from the Fernseher class which is the first class i make.Then in main i create the Object in the FernseherRemote class.
then trying to check the status of the tv, if it is on or off, it is initially off so i use the tv.setStatus(sum) to turn it on sum = true;then i use the tv.getStatus(sum) to return the "sum" and it is back to false??? i'll paste the code so you all can see it and maybe give me a hint as to why the "sum"= false after setting it...

first the Fernseher class,,



and here is the FernseherRemote class,, i have the "System.out.println("sum ="+sum+" is here too!");" to see the sum (true or false) to see if the correct sum gets to those points, which they don't.




Thanks for any tipps
Mike
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Quoting the relevant code:
mike ryan wrote:


and here is the FernseherRemote class,, i have the "System.out.println("sum ="+sum+" is here too!");" to see the sum (true or false) to see if the correct sum gets to those points, which they don't.



Not sure if I got your doubt correctly.

You need to assign the value returned by setStatus to the sum variable. As it is a pass by value for primitives, the value of sum changed in the setStatus is not reflected in the FernseherRemote class. So I think changing
to might solve the problem.

Actually the APIs are not designed correctly-

void setStatus(boolean)
boolean getStatus()

Mohamed Sanaulla | My Blog
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Everything is perfectly fine..Except you have made some compile time error.but as you have mention that you code is running so i assume that you have made error while posting this code on javaRanch.

Just declare the sum as globally..

Like


and see the magic


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

and one more thing setStatus should be void

so convert it into


There should not be any return statement in the setStatus..
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Shanky Sohar wrote:Everything is perfectly fine..Except you have made some compile time error.but as you have mention that you code is running so i assume that you have made error while posting this code on javaRanch.

Just declare the sum as globally..

Like


and see the magic


Making it global- Is it a good practice? I just zeroed in on the possible flaw and then told the solution. Going by the design of classes- They need to be fine tuned to follow the OO Principles.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

I understand what you say,but in case of JavaBeans.
For getter and setter methods you have to declare a variable,which is common to both,
so globally declaring the variable is the best choice
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

hi,

Thank you both for the quick replies, and i tried both versions as well as a mix of both...and still not working? the sum is set to true in the set method and when calling directly after the getStatus method it still comes back false immediately? When i make the variable Global, i still need to initialize it in the other class as false either locally or as an instance variable it is automatically set to false i believe? Is there anything else anyone sees that may be causing my problems?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Beware of long lines, especially in code tags; they make the code difficult to read.
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
In Java, primitive arguments are passed "by value" not "by reference." The thing you are passing (boolean sum) is a boolean primitive value.
Other issues with the code aside (why is a 'set' method returning a non-void value and why does your 'get' method accept an argument?) you might try something like


However, when you do this next:



sum is always going to end up being simply a copy of whatever value you passed in to the getStatus() call.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Alex Hurtt wrote:In Java, primitive arguments are passed "by value" not "by reference." . . .
That should read
In Java, all arguments are passed "by value" not "by reference."
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

ok i have tried doing it a few different ways and still getting boolean sum=false?

Here is the code as it currently is





and the Fernseher class







any ideas? or is something more incorrect than i thought??

Mike
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

mike ryan wrote:ok i have tried doing it a few different ways and still getting boolean sum=false?

Here is the code as it currently is





and the Fernseher class







any ideas? or is something more incorrect than i thought??

Mike


Where ever you are referring to the sum instance variable of class Fernseher- Use this.sum (to differentiate between the sum passed via the method parameters and the instance variable.

The order of methods being called can be changed to- setter (setStatus) and then getter(getStatus). You need not pass the any boolean to the getStatus. Instead in the getStatus() you could simply return this.status.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

ok ya kind of lost me there??
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi again,

OK i got it to work now but it didn't work for me with this.on but rather with (Fernseher.on ----> i changed sum to on cause it seemed more appropriate) in the set and get methods and also in the FernseherRemote class.
Maybe i was using the "this.on" incorrectly not sure.I can post the code as well if anyone is interested in seeing it.

Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about using boolean to check a condition of an object?
 
Similar Threads
using user input to pick Object to be used in case/Switch
How to use canceled button in JOptionPane
Problem with indexing of an array
Problems with "while " loops
help with joption pane cancel button