-1

Is it bad to pass instances of objects between methods in a class? Or is there a better way of doing this?

public void game() {
    Deck deck = new Deck();
    shuffleDeck(deck);
}

public void changeCard(Deck deck) {
    deck.replaceCard(deck);
}
Bryan
  • 14,756
  • 10
  • 70
  • 125
Danielh112
  • 15
  • 1
  • 5
  • i do it myself, i don`t think its bad. other method would be member variables – XtremeBaumer Nov 16 '16 at 13:34
  • 2
    I'd say this is one of the features which makes this kind of programming to object oriented programming – Arthur Eirich Nov 16 '16 at 13:35
  • What could be considered bad in your example is that the method modifies its parameter. This could be considered a side-effect, too much of this will make your code difficult to understand. – Tobb Nov 16 '16 at 13:37
  • There's no reason why passing objects from one method by another, in general, would be bad. – Jesper Nov 16 '16 at 13:37

1 Answers1

0

Well... passing class object would be quite difficult in . On the other hand the in-build mechanism of passing value of the reference is very useful and we all do that daily.

Is Java "pass-by-reference" or "pass-by-value"?

Operating with objects is a main principle of Object Oriented Programming and Java is object oriented.

Remember that you don't pass the whole object. You can imagine that as passing the instructions on how to find that object in memory.

Community
  • 1
  • 1
xenteros
  • 15,586
  • 12
  • 56
  • 91
  • @Danielh112 just in case you want to ask more questions in the future, I strongly recommend reading [ask] and [mcve]. These are interesting articles about how to write not to get downvoted. – xenteros Nov 16 '16 at 14:06