0
public class Card
{
   private Rank aRank;
   private Suit aSuit;

   public Card( Card pCard )
   {
      aRank = pCard.aRank;
      aCard = pCard.aSuit;
   }
   ...

I'm a bit confused. The references happen between members of the same class, but I thought the only way you can access the private variable without getters is that it references the private variable within the method of the same class, and it has to be within the caller's scope(If that makes sense? Meaning we can only access the caller's instance variables). But in this case, pCard is not the caller of the constructor; it's just an another object that is passed as a parameter. So I was expecting something like:

public class Card
{
   private Rank aRank;
   private Suit aSuit;

   public Card( Card pCard )
   {
      aRank = pCard.get_aRank();
      aCard = pCard.get_aSuit();
   }
   ...
Ted
  • 469
  • 4
  • 16
  • 3
    related: https://stackoverflow.com/questions/27482579/how-is-this-private-variable-accessible – Eran Feb 07 '18 at 09:53
  • There has to be a dupetarget for this. :-) What matters isn't what *instance* the property is on, what matters is that the code accessing it is in the same class as the instance. It's perfectly fine for one instance of a class to access the private data/methods of another instance of that same class (and in fact, it's sometimes important for implementing `equals`). – T.J. Crowder Feb 07 '18 at 09:54
  • [_Controlling Access to Members of a Class_](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html), not instance, class. You are still in the same class since `pCard` is a `Card` too. How could you clone an instance with private field if you could not access them ? – AxelH Feb 07 '18 at 09:55
  • Getters are the standard method if getters can access a private member then any other method can access. – Amit Bera Feb 07 '18 at 09:56

0 Answers0