Wednesday, 15 July 2015

java - Will I be able to modify two or more object instances using another object variable? -


I am writing a simple two-player game console application, and I should know that I can do the following from the following I am I have been confused with whole Java pass-by-value / reference articles ....

  Player p1 = new player ("John"); Player p2 = new player ("Sam"); Player Turns = P1; While (p1.life> 0 & amp; amp; p2.life & gt; 0) {current.doSomething (); If (current .name.equals (p1.name)) {current = P2; } And {current = P1; }}  

I suspect that this variable is current that can be modified, but p1 or p2 < / Code>. These are two examples that I want to modify. I just do not want to repeat a whole code block for each player, which results in a very heavy if-and statement.

Your apprehension is wrong .. current , p1 and p2 are all references . There are 2 examples and 3 references in your case. Therefore, the example can be pointing to current and can be modified and it can be modified to p1 or p2


No comments:

Post a Comment