Java Object Reference and Java Methods -


I am unable to understand how it works

  public Zero addToRule ( Rule r) {If (GetRuleList (.) Contains (R)) {getRuleList (). AddElement (r); }}   

If I run this code:

  obj.addToRule (r); Println (. GetRuleList () included (R));   

It prints true how can it be?

btw ruleList is a vector member of the main category and this is not a stable variable (does not seem to be so but sharing anyway).

  import java.util.Vector; Public Class RuleEngine {Private Vector Rule List = New Vector (); Public vector getRuleList () {return rule list; } Public Zero AddToRule (Rule R) {If (GetRuleList (.!) (R)) getRuleList (). AddElement (R); } Public static zero major (string algos []) {RuleEngine re = new RuleEngine (); Rules R = New Rule ("Rule 1"); Re.addToRule (r); Println (.re.getRuleList () includes (R)); }} Class rules {public string name = ""; Public rule (string name) {this.name = name; }}   

Okay people have told me that this works because of passage in the context of Java. I got it. But instead of its context, what can I do to get a copy of that item?

From your comments it seems that you have not fully understood that there is a value and context in Java What is the difference between Originally, the objects are always passed as references in Java.

Consider

  class test {Private list list = new Arrayist (); Public list getlist () {return list; }}   

getList () method will return a reference to the list object. This will return a copy of the no list object

  test test = new test (); String S = "ABC"; . Add test.getList (); Println (. Test.getList) contains (s));   

The first time the getList () is called, a give feedback is returned to the list, since then the truth will return The second time getList () is called, it returns context to the same list, which is the junk (s) is applicable; That's a copy, not a new list - the same reference. s was added, on which s was added.

Consider this, though

  test test 1 = new test (); Test test2 = new test (); String S = "ABC"; Test1.add (s); Println (. Test2.getList () includes (s));   

Why would this "false" print? Returns a reference to the list inside test1.getList () test1 and references the test2.getList () list to test2 . Here, s was added to the test1: s list, so it will not be included in the test2: s list.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -