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 10 days ago.
Improve this question
My probleme is how to change a global class variable in a void function. I need to conserve the changes of my classe. How can I do that ?
//for example
Class Animal{
B b = null;
void Treatement(B b)
{
try{
b = new Class B()
b.name = "toto";
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
}
}
}
The probleme is I can not find a solution for this example because my variable has to be as parametre and need to save it for other fonctions when an exception is raised.
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 1 year ago.
Improve this question
I have a method:
public void dealDmg(int hp) {
this.hp = hp;
this.hp -= this.dmg;
}
But when i use it as dmg_dealer.dealDmg(dmg_receiver.hp);, it doesn't work properly as in the dmg_receiver's hp is not decreased, but the dmg_dealer's hp becomes of the same amount as of the dmg_receiver, and then dmg_dealer's hp gets reduced by the dmg amount. How can I change a body of the method so that it works in such a way?
You need to pass in the dmg_receiver object to the method instead of dmg_receiver.hp, that way you can update hp in dmg_receiver as well. The updated method should look like below:
public void dealDmg(DmgReceiver dmg_receiver) {
this.hp = dmg_receiver.hp;
dmg_receiver.hp -= this.dmg;
}
If the object represented by this is dealing damage to another object, the dealDmg needs to the object being dealt damage to (assuming that dmg is a member):
public void dealDmg(Unit other) {
other.hp -= this.dmg;
}
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
I have this code:
entity.conditions().forEach(entityCondition - > {
if (entityCondition instanceof LinkCondition) {
LinkCondition condition = (LinkCondition) entityCondition;
} else if (entityCondition instanceof OppositeLinkCondition) {
//...
} else if (entityCondition instanceof PropertyEqualCondition) {
//...
} else if (entityCondition instanceof PropertyLocalTimeRangeCondition) {
//...
}
// and 20 or so, more conditions...
});
In which I am looking for an alternative than IF-ELSE statement, a more elegant approach in dealing with dozens of instanceof conditions.
Unfortunately, there is no such syntax in Java yet. You can try Kotlin which already supports this:
val b = when (a) {
is String -> a.length
is Int -> a
is Long -> (a and 0xFFFFFFFF).toInt()
else -> a.hashCode()
}
As #Progman pointed out, this question is similar to other questions, you should check them out too.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Why does java.lang.Exception not provide a setter for the message and allow setting it only via the constructor (e.g. super(_msg))?
For example:
public BadParameterException(QueryParameter _param, String _valStr) {
this.param = _param;
this.valAsStr = _valStr;
}
public BadParameterException(QueryParameter _param, String _valStr, String _msg) {
this(_param, _valStr);
/* This is not possible */ super.setMessage(_msg);
}
instead, I have to do this:
public BadParameterException(QueryParameter _param, String _valStr, String _msg) {
super(_msg);
this.param = _param;
this.valAsStr = _valStr;
}
Because an Exception is a snapshot of a situation. It's not supposed to change it's state.
Since the message can only be set through the constructor, it's basically final and cannot be changed afterwards. That's how it was designed.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have this code in Java.
MyObject a = MyObject.newMyObject();
a.setParameter(Parameters.BASIC);
CompositeObject co = CompositeObject.newCompositeObject();
co.add(BasicFactory.newInputStream(new FileInputStream(file), ResourceType.BASIC, a);
Is it possible to do it better using Groovy?
Here's a start.
def a = MyObject.newMyObject()
a.parameter = Parameters.BASIC
def co = CompositeObject.newCompositeObject()
co.add(BasicFactory.newInputStream(new FileInputStream(file), ResourceType.BASIC, a))
If all you really need is the co then you can do something like this to limit the scope of temporary variables.
def co = CompositeObject.newCompositeObject().with {
def a = MyObject.newMyObject()
a.parameter = Parameters.BASIC
def factory = BasicFactory.newInputStream(new FileInputStream(file), ResourceType.BASIC, a)
co.add factory
return it
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hey guys so I have the method
public void addQuestion(Question question)
{
}
I have created an ArrayList in the class section
private ArrayList<Question> quizQuestion;
and I have initialised the ArrayList in the constructor by typing
quizQuestion = new ArrayList<Question>();
What do I type in the method above so I can add questions to the Question ArrayList. :)
public void addQuestion(Question question)
{
quizQuestion.add(question);
}
public void addQuestion(Question question)
{
quizQuestion.add(question);
}
Not sure, what you really want :)
but if you just want to add parameter Question to arraylist quizQuestion
quizQuestion.add(question);
I hope this helps...
Keep Leanrning :)