File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Boolean and boolean problems in JavaBeans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Boolean and boolean problems in JavaBeans" Watch "Boolean and boolean problems in JavaBeans" New topic
Author

Boolean and boolean problems in JavaBeans

Sloan Bowman
Ranch Hand

Joined: Jan 21, 2003
Posts: 107
I am trying to create a simple JavaBean for a website and I can't seem to get Booleans to work properly. Below is a demo code example
public class Users
{
private Boolean allow;
public Users
{
} // end Users() constructor
public void setAllow()
{
this.allow = true;
} // end setAllow()
public Boolean getAllow()
{
return(this.allow);
} // end getAllow()
} // end class

This is a brief example let me know if you have any ideas. I would greatly appreciate your help.
--Sloan
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2849
    
  11

Ah, that's easy. Don't capitalize boolean.
boolean is a Java primitive type that can contain the values true and false.
Boolean is a class that wraps a boolean primitive. It's only useful when you need to use an object instead of a primitive.
Sloan Bowman
Ranch Hand

Joined: Jan 21, 2003
Posts: 107
I've tried that too with no luck unfortunatly. I'll past in the actual code.
package com.sports.workorders;
public class Users
{
// Declare Global Variables
private String username;
private String password;
private boolean allow;
public Users()
{
}
public void setUsername(String username)
{
this.username = username;
}
public String getUsername()
{
return(this.username);
}
public void setPassword(String password)
{
this.password = password;
}
public String getPassword()
{
return(this.password);
}
public void setAllow()
{
this.allow = true;
}
public boolean getAllow()
{
return(this.allow);
}
}
Tom Purl
Ranch Hand

Joined: May 24, 2002
Posts: 104
What error messages are you getting? What do you mean when you say it doesn't work?


Tom Purl<br />SCJP 1.4
Sloan Bowman
Ranch Hand

Joined: Jan 21, 2003
Posts: 107
I was getting a null/false entry every time I tried to get the value of the boolean. I made a modification to the code to do this and it works fine now.
public void setAllow(boolean s)
{
this.allow = s;
} // end setAllow()
Now when I use the Bean inside is JSP it is working properly.
<jsp:useBean id="users" class="com.sports.workorders.Users" scope="session"\>
users.setAllow(true);
users.getAllow(); now returns true where before it would return false regardless if I called the method setAllow() or not.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Hi Sloan LastName,

Welcome to JavaRanch! Please adjust your display name to meet the JavaRanch Naming Policy.
You can change it here.

Thanks!


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
By the way, a last initial is not considered to be a valid last name.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2849
    
  11

I think you're following a red herring here. Your first example would not compile. First, because you're missing parentheses in the constructor. Even assuming that is just a typo, the statement:
this.allow = true;
does not compile if allow is defined as Boolean. You say you got a "null/false" result back, so it follows that you got something to compile, but it wasn't that. When you switched from Boolean allow, to boolean allow, you probably still picked up this hypothetical class during your run. When you rewrote the setAllow() method, you recompiled correctly and started picking up your new class. That's my guess anyway. Either setAllow() method, as you've written them here, would work fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boolean and boolean problems in JavaBeans