Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boolean and boolean problems in JavaBeans

 
Sloan Bowman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error messages are you getting? What do you mean when you say it doesn't work?
 
Sloan Bowman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sloan LastName,

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

Thanks!
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, a last initial is not considered to be a valid last name.
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic