Transient Boolean Variable Issue - java

I have a transient variable that does not exists in the database, it's an interpretation for another variable (integer variable), basically if that int variable equals 2, my transient is True, else it's False. This is my getter and setter:
public Boolean getAceitaPix() {
if(versaoWS.equals(2)){
this.aceitaPix = true;
}else{
this.aceitaPix = false;
}
return aceitaPix;
}
public void setAceitaPix(Boolean aceitaPix) {
if(this.aceitaPix == true){
this.versaoWS = 2;
}else{
this.versaoWS = null;
}
this.aceitaPix = aceitaPix;
}
The issue is, when I try to make the changes at my XHTML, the getter is called again and it changes the boolean no matter what I do at the Form, so when the setter is called the transient variable is always TRUE. How do I proceed? Do I make another Boolean that only changes the setter? I think there's a smarter way of doing this.

Sounds like you don't need a field there. Instead, you could just have a getter that calculates it on the fly:
public boolean getAceitaPix() {
return versaoWS.equals(2);
}

Related

How to return a boolean from a function in java? getting an error

public class Test {
public boolean checkx(boolean x) {
boolean status;
if (x) {
status = true;
}
return status;
}
}
Error: The local variable status may not have been initialized
I don't know what I was doing wrong, code looks alright to me.
In the Java Language Speciication, it is written that:
A local variable (§14.4, §14.14) must be explicitly given a value
before it is used, by either initialization (§14.4) or assignment
(§15.26), in a way that can be verified using the rules for definite
assignment (§16 (Definite Assignment)).
So, you must have to initialize or assign a value to the local variable before using it.
In your particular example:
public boolean checkx(boolean x) {
boolean status = false;
if (x) {
status = true;
}
return status;
}
You're doing it way to complicated. You can check the status of a boolean by writing:
if(x == true)
// Do Something
or even simpler:
if(x)
// Do Something
Or you can write a function to get x from another class. In case the x is private:
public boolean getX() {
return x;
}

What are some best practices for a creating a constructor that takes a boolean isX and depending on that value would need a boolean for isY?

In this hypothetical problem I'm creating a class to represent Sausage which can be:
A. Fresh and Packaged
B. Fresh and Not Packaged
C. Not Fresh and Not Packaged
Note: It can't be Not Fresh and Packaged.
I'm looking for the best practice to create constructor with this in mind.
The way I tried is below, but I think there should be a better solutions.
public class Sausage {
Meat meat;
boolean isFresh;
boolean isPackaged;
public Sausage(Meat meat, Boolean isFresh, Boolean isPackaged) {
this.meat = meat;
if (!isFresh) {
this.isFresh = false;
this.isPackaged = false;
}
else if (isPackaged) {
this.isFresh = true;
this.isPackaged = true;
}
else {
this.isFresh = true;
this.isPackaged = false;
}
}
}
I'm looking for a cleaner way to provide this functionality.
You can represent the possible states as an enumeration type:
enum SausageType {
FreshPackaged,
FreshUnpackaged,
NotFresh;
}
Then change your constructor to take a SausageType instead of two Booleans.
The advantage of this method is that it is not possible to call the constructor with the wrong combination of states, it is obvious to users of your class what the possible values are, and you do not need to remember to keep brittle runtime checking of arguments up to date with changing requirements.
There are few problems with your current approach:
There is a way to create write new object which makes no sense e.g. new Sausage(..., false, true) where packaged will be silently converted to false. This is confusing and makes code harder to understand.
You are mixing Boolean object type and boolean primitive type which will result in unnecessary auto-boxing. If you don't need to support null use boolean.
boolean fields probably don't need is prefix.
Since there are two boolean flags and 4 possibilities they can be set (true true, true false, false true, false false) but only 3 valid options perhaps named factory methods would be more suitable:
public class Sausage {
public static Sasuage newFreshPackaged(Meat meat) {
return new Sasuage(meat, true, true);
}
public static Sasuage newFreshNotPackaged(Meat meat) {
return new Sasuage(meat, true, false);
}
public static Sasuage newNotFreshNotPackaged(Meat meat) {
return new Sasuage(meat, false, false);
}
private Sausage(Meat meat, boolean fresh, boolean packaged) {
this.meat = meat;
this.fresh = fresh;
this.packaged = packaged;
}
}
Silently changing isPackaged to false may be a surprise to calling code. Here, you may choose to throw an exception if the combination (not fresh, packaged) is chosen.
if (!isFresh && isPackaged) {
throw new IllegalArgumentException("Can't be both not fresh and packaged!");
}
this.isFresh = isFresh;
this.isPackaged = isPackaged;
You may also decide to use factory methods (making your constructor private) that enforce your requirements. This would avoid needing an exception to be thrown.
public static Sausage createFreshSausage(Meat meat, boolean isPackaged) {
return new Sausage(meat, true, isPackaged);
}
public static Sausage createNotFreshSausage(Meat meat) {
return new Sausage(meat, false, false);
}
Incidentally, normally the primitive boolean would be used in the constructor's parameters' types, not the object wrapper Boolean, like your instance variables. A null value here that would be allowed by Boolean doesn't make any sense.

If statement Object boolean not working

I'm trying to check for a persons age using another object in another class, for some reason the areThey boolean returns false no matter what. The following code that I implemented is_
public class main {
public static void main(String[] args){
Obj object = new Obj();
object.areTheyOldEnough();
if(object.areTheyOldEnough() == true){
System.out.println("They are old enough!");
}else{
System.out.println("They are not old enough!");
}
}
}
public class Obj {
private int age = 15;
private boolean areThey;
public boolean areTheyOldEnough() {
if (age > 12) {
boolean areThey = true;
}
return areThey;
}
}
Your problem is called shadowing; here:
That most inner declaration of :
if (age > 12) {
boolean areThey = true;
Is simply wrong, that should read:
areThey = true;
instead. The point is: you are declaring a new variable of that name, and give that the value true. But that variable vanishes in thin air when that if-block is "left" ... and what is returned is the value of the field areThey within your obj class. And that one still has its initial default value of false.
And beyond that: naming is a real problem in your code. Use names that A) comply to java coding standards; so class names start UpperCase for example; B) mean something.
In other words: the name Object doesn't mean anything (besides creating yet another name clash with the java.lang.Object class name). Better call it "testInstance" or something like that - as said: use names that mean something.
You have two boolean variables of the same name - one is a local variable, which you are setting in your method (inside the if block), and the other is a an instance variable which you are returning.
You don't need any boolean variable, just write :
public boolean areTheyOldEnough() {
return age > 12;
}
you mixed visibilities of field and local variable
public class obj {
public int age = 15;
public boolean areThey; //initialized to false as default value for boolean
public boolean areTheyOldEnough() {
if (age > 12) {
boolean areThey = true; //create new local variable with the same name as field,
// but visible just in scope of if-block
}
return areThey; // return value from field
}
correct your code as follow :
public class main {
public static void main(String[] args){
obj Object = new obj();
if(obj.areTheyOldEnough()){
System.out.println("They are old enough!");
} else{
System.out.println("They are not old enough!");
}
}
and your obj class will be :
public class obj {
public int age = 15;
// it is not required, but if you need for other methods,
// you can define it
public boolean areThey;
public boolean areTheyOldEnough() {
return age > 12;
// or if you need set variable areThey, so use following codes
/*
areThey = age > 12;
return areThey;
*/
}

Can I use value of a local variable defined in a method, in other method in Java?

The method is not returning the value of the local variable.
Can I use the value of local variable index from the following method
public boolean contains(Object input) {
int index = 0;
while(myAsetIterator.hasNext()) {
index++;
if(input.equals(myAsetIterator.next())) {
return true;
}
}
return false;
}
in this method as the index of the array of the object that I want to remove.
public boolean remove(Object o) {
int count = 0;
if(o == null) {
return false;
}
if(contains(o)) {
genArray[index] == null;
}
if (count > 0) {
System.out.println(count+" same elements were present in Aset. "
+ "Removed all those "+count+" elements from Aset.");
return true;
}
return false;
}
I know the scope of a local variable is limited to the method it's declared in. But there might be a way that I might not now yet to make this happen without using a field/instance variable.
No. The whole point of it being local to a method is that it only exists within that method. The options are:
Use an instance field, i.e. make it part of the state of the object. That's unlikely to be appropriate.
Use a static field, i.e. make it part of the static of the type. That's almost certainly inappropriate.
Change the existing method to return the information you want.
Create a new method to return the information you want.
Duplicate the existing code within remove so that you can get the index. That would be sad :(
As an example of the last two, you could write:
public int indexOf(Object input) {
int index = 0;
while(myAsetIterator.hasNext()) {
index++;
if (input.equals(myAsetIterator.next())) {
return index;
}
}
return -1;
}
public boolean contains(Object input) {
return indexOf(input) == -1;
}
... then in your remove method, you'd use indexOf instead of contains.

How to use an "is" method

I have to create an object that uses an "is" method, basically declaring the state of the object. I am not sure how this should work. Right now am writing the method as a boolean but I am wondering if I should use a different approach, here is the code,
public class Cell
{
public int move;
public Cell(int xmove)
{
xmove = 0;
}
public boolean isempty(int x)
{
if(x == 0)
{
return true;
}
else
{
return false;
}
}
}
You are kind of on the right track but there are a bunch of issues.
First, this is much simpler
public boolean isEmpty(){
return move == 0;
}
I assumed that an instance of Cell is empty if its move is 0.
Note that I've camel cased your method name. Also, isEmpty is supposed to say something about the state of an object. It doesn't make sense to pass in x (unless you want to compare x to some property on the object instance).
Second, you constructor takes an argument, then sets it to 0. That's not going to do anything. You probably want
public Cell(int move){
this.move = move;
}
Which takes an argument and sets the field on the current instance that is being constructed to the value passed in (you defined a field move, so you probably want to set it.).
So you could do something like
Cell cell1 = new Cell(1);
Cell cell2 = new Cell(0);
cell1.isEmpty() // false;
cell2.isEmpty() // true;

Categories

Resources