Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Wasn't sure how to search for this and was unable to find the answer to set the following to true (Java):
boolean flagRefund = true;
if (flagRefund){
// Suppose to set wasRefunded field for user in DB to true or 1
dbModelUser.getWasRefunded();
}
This is a lesson to teach you how to read the code if you see the method that starts with "get" then you use it to get some value from the method that returns it. When the method starts with "set" then this method usually returns void and takes parameters. Like this
boolean flagRefund = true;
if (flagRefund){
// Suppose to set wasRefunded field for user in DB to true or 1
dbModelUser.setWasRefunded(flagRefund);
}
Usually, get methods are used to retrieve information, not to set information. Look if there is some set in your code method to achieve it.
Also, you don't pass any value to your method getWasRefunded as parameter so it is impossible that it could set any info.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Good I am trying to create a function that generates new Array, and trying to use a getter method that returns a name for example ArrayD5, ArrayD10 and use these as the name of the new arrays to generate.
I have tried to do this:
int length = 5;
String (SeatIbiza.nombres(length))[] = new String[length];
String nombres (int length) {
return "ArrayD"+length;
}
If you are looking for a way to declare something like e.g.
String "ArrayD" + 2
so that you have a variable
String ArrayD2
then this is not possible in Java. Dynamic naming of variables is not supported.
I don't think if that's possible. The variable names are required to be declared during compile time in JAVA and dynamic naming isn't supported... yet.
Correct me if I'm wrong as I'm still learning about JAVA
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I saw below code in some codebase, and I am not sure how we can return Optional for a String return type.
public String generateTemplate() {
Optional<String> template = Optional.ofNullable(lenderAndTemplateMap.get("LENDER_1"));
return template.orElse("TEMPLATE_2");
}
Although the line reads as "return template [if possible], or else return "TEMPLATE_2"", the Optional itself is never actually returned. It's just a call to Optional.orElse(), which returns the internal value if it's present, otherwise it returns the value passed in. Both are of type String, therefore it's a valid return statement.
As a side note, in your specific example Optional isn't really necessary. You can use a simple null check or Map.getOrDefault():
return lenderAndTemplateMap.getOrDefault("LENDER_1", "TEMPLATE_2");
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to set a property to an other property that could be null. I wonder what is faster, to wrap it around an if statement or simply just set the property without the check.
with if statement
if (foo.getName() != null) {
bar.setFoo(foo.getName());
}
simple
bar.setFoo(foo.getName()); //getName() can be null
If you are going to setFoo() either way, you might as well just set it and skip the condition. I would use the condition if you wanted to take an action if !null and a different action if null.
when foo.getName() != null would return true, the setter would be called, so you will have 2 commands that will be executed, however the second example will execute only 1 command. I think the second one is more performant
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is possible to do in Java something like this? :
System.out.... -> ask for a string
Store this string
Create a variable with the name of this string
Thank you very much!
No. You cannot create a variable, at execution time, with a particular name.
However, you can use a Map, which probably does what you want. The get method will look up an entry, and the put method will set an entry. Example:
Map<String, Integer> myMapOfThings = new HashMap<>();
String nameOfThing = /* ... get a string from the user somehow here ... */;
myMapOfThings.put(nameOfThing, 2);
System.out.println(myMapOfThings.get(nameOfThing)); // prints 2
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have tried to return null as third value in my boolean function, but it won't compile. I need to return three values from my class method - true, false and null (for example). Is there any standard way how can I do it?
Please use an enumeration with three values defined. Hacking things together is no solution.
Similar question has been asked, it should help.
You can make your own POJO object with this logic in getXX() method. From your method return this POJO with value and test it in code.
Generaly, don't use null values as state indicators.