No matter the type of a variable or whether it is inmutable or not, if you reassign a variable inside a method you are not going to see that change outside the method. Each method in java receives a copy of the variable (pointing to the same object of course) we are passing. Changing the object reference of the variable inside a method is not going to affect the original variable.<div>
<br></div><div>@Abhinay, the truth is that Drools transforms the RHS of a rule is into a java class. And, just like Wolfgang says, globals are stored in some kind of Map, so you must use setGlobal() if you want to change the value of a global in drools.</div>
<div><br></div><div>Best Regards, <br clear="all"><br>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br><br>Esteban Aliverti<br>- Blog @ <a href="http://ilesteban.wordpress.com" target="_blank">http://ilesteban.wordpress.com</a><br>
<br><br><div class="gmail_quote">On Mon, Jul 16, 2012 at 2:08 PM, Wolfgang Laun <span dir="ltr"><<a href="mailto:wolfgang.laun@gmail.com" target="_blank">wolfgang.laun@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
It may not be quite so simple as that - using int to illustrate a<br>
problem occuring with Integer is misleading.<br>
<br>
An Integer is an immutable object.<br>
Integer a = Integer.valueOf( 42 );<br>
a += 1<br>
You'll certainly still have an Integer object available via 'a', but<br>
it's *not* the one containing 42 any more - that's already in the<br>
garbage.<br>
<br>
The situation is aggravated by the fact that a 'global' is stored in a<br>
sort of container and this container is not directly accessible for<br>
effecting a change, only via setGlobal().<br>
<span class="HOEnZb"><font color="#888888"><br>
-W<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
On 16/07/2012, Esteban Aliverti <<a href="mailto:esteban.aliverti@gmail.com">esteban.aliverti@gmail.com</a>> wrote:<br>
> Let's say you have this method in a java class:<br>
><br>
> public void addOne(int number){<br>
> number = number +1;<br>
> }<br>
><br>
><br>
> And let's say you want to invoke that method from another method in the<br>
> same (or a different. It doesn't matter) object:<br>
><br>
> int number = 0;<br>
> this.addOne(number);<br>
> System.out.println("Number= "+number);<br>
><br>
> What is the output of the code above? Figure out that and you will figure<br>
> out what is happening with your global.<br>
><br>
> Best Regards,<br>
><br>
> XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br>
><br>
> Esteban Aliverti<br>
> - Blog @ <a href="http://ilesteban.wordpress.com" target="_blank">http://ilesteban.wordpress.com</a><br>
><br>
><br>
> On Mon, Jul 16, 2012 at 11:53 AM, abhinay_agarwal <<br>
> <a href="mailto:abhinay_agarwal@infosys.com">abhinay_agarwal@infosys.com</a>> wrote:<br>
><br>
>> global java.lang.Integer count<br>
>><br>
>><br>
>> rule "rule1"<br>
>> agenda-group "1"<br>
>> when<br>
>> //smething<br>
>> then<br>
>> count=0;<br>
>> System.out.println(count); // output is 0<br>
>> count = count +1;<br>
>> System.out.println(count); // output is 1<br>
>> drools.setFocus("2");<br>
>> end<br>
>><br>
>><br>
>> rule "rule2"<br>
>> agenda-group "2"<br>
>> when<br>
>> //smething<br>
>> then<br>
>> System.out.println(count); // output is 0<br>
>> end<br>
>><br>
>> in the rule2, the value of the global variable "count" must be 1, why m i<br>
>> getting it as zero ?<br>
>><br>
>> --<br>
>> View this message in context:<br>
>> <a href="http://drools.46999.n3.nabble.com/Simple-Global-Error-tp4018697.html" target="_blank">http://drools.46999.n3.nabble.com/Simple-Global-Error-tp4018697.html</a><br>
>> Sent from the Drools: User forum mailing list archive at Nabble.com.<br>
>> _______________________________________________<br>
>> rules-users mailing list<br>
>> <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
>> <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
>><br>
><br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
</div></div></blockquote></div><br></div>