Matheus,
pelo que entendi do exemplo que vc deu na postagem, o objeto está sendo passado sim por referência. Veja só. Qdo vc chama o método foo vc cria um objeto o e fala que ele é uma referência para o carinha que vc está chamando. Tanto é q se vc fizer o.setNome(“segundo objeto”) ao invés de o = new Objeto(“seg obj”), qdo a função retornar a saída será:
primeiro objeto
segundo objeto
Isso é passagem por referência, não tem nada de valor aí.
No seu caso, qdo vc faz o = new Objeto(“seg obj”) vc simplesmente disse q o Objeto o não está mais “apontando” para o carinha q foi passado como parâmetro, e sim para um novo objeto. Então nem tinha q imprimir segundo objeto mesmo, tá certo, passagem por referência da mesma forma.
Só pra lembrar, o método foo é:
private static void foo( Objeto o )
{
o = new Objeto(“segundo objeto”);
}
Segue o começo do texto do Apendice A do livro Thinking in Java, 4ª edição. Tem na net, de graça…
A: Passing & Returning Objects
By now you should be reasonably comfortable with the idea that when you?re ?passing? an object, you?re actually passing a reference.
In many programming languages you can use that language?s ?regular? way to pass objects around, and most of the time everything works fine. But it always seems that there comes a point at which you must do something irregular, and suddenly things get a bit more complicated (or in the case of C++, quite complicated). Java is no exception, and it?s important that you understand exactly what?s happening as you pass objects around and manipulate them. This appendix will provide that insight. Feedback
Another way to pose the question of this appendix, if you?re coming from a programming language so equipped, is ?Does Java have pointers?? Some have claimed that pointers are hard and dangerous and therefore bad, and since Java is all goodness and light and will lift your earthly programming burdens, it cannot possibly contain such things. However, it?s more accurate to say that Java has pointers; indeed, every object identifier in Java (except for primitives) is one of these pointers, but their use is restricted and guarded not only by the compiler but by the run-time system. Or to put it another way, Java has pointers, but no pointer arithmetic. These are what I?ve been calling ?references,? and you can think of them as ?safety pointers,? not unlike the safety scissors of elementary school?they aren?t sharp, so you cannot hurt yourself without great effort, but they can sometimes be slow and tedious. Feedback