Question about method call and creating a new object within an object
I'm a Java virgin. How do you read:
(current.getPlayer() ).getID() < p.getID() )
Are 2 mthod calls happening at once?
I also have a seperate question. If you are creating a new object within a new object such as
Playernode nd=new Playernode(new Player(p));
which is perfectly legal, isn't the first object of Player class which doesn't seem to have a direct object reference in danger of garbage collection off 开发者_StackOverflow社区the heap?
I'm a Java virgin. How do you read: (current.getPlayer() ).getID() < p.getID() )
It's a relational expression between two sub-expressions, evaluating to either true or false.
The first variable is
(current.getPlayer() ).getID()
The second variable is
p.getID()
You'll have a compiler error, because there's an extra right paren.
Are 2 mthod calls happening at once?
No, there are three: one for getPlayer() on the current instance, a second to getID() on that Player instance, and the third to getID() on p, which I assume is another instance of Player.
"at once" makes me think about threading. In your case, this is all happening on a single thread in serial fashion.
I also have a seperate question. If you are creating a new object within a new object such as Playernode nd=new Playernode(new Player(p)); which is perfectly legal, isn't the first object p of Player class which doesn't have a direct object reference in danger of garbage collection off the heap?
The parameter you're passing to the Player (copy) constructor is a reference, so it won't be gc'd. If you assign a private data member in the Playernode class to point to the new Player, it will point to the new Player reference. Looks like you initialize it to have the same state as p, even if it doesn't point to the same reference.
No danger of being gc'd.
精彩评论