I’m sorry if this title doesn’t describe the problem properly but I wasn’t sure how to describe it.
I have a method called changeToWhite() that can be called on a Piece
e.g. Piece.changeToWhite()
but for me to be able to change the piece to white I need access to the piece so I decided to pass it in as an argument
e.g. Piece.changeToWhite(Piece)
The passing in as an argument seems unnecessary.
The toUpperCase() functions does it somehow e.g. someString.toUpperCase()
How can I do this?
If you define
changeToWhiteon aPiececlass, then on an instance you can usethisto get the reference to the current object. Something like:Note that when you define a method on a class, you need to have an instance on which to call the methods.
So
Note that the Java standard is to use uppercase to define a class (e.g.
Piece), and camel case (e.g.changeToWhite) for instance methods and fields.The OTHER way to do it would be to use a static method. In this case, the method belongs to the class, it doesn’t have a
thiscontext like instance methods dobut the first way is preferable.