0

Say for example I have four classes A, B, C and D; where D's constructor takes A, B and C as parameters. If I have the following implementation:

public static main(String[] args) {
  A = new A();
  B = new B();
  C = new C();
  D = new D(A, B, C);
}

And the instance variables for D are:

  private A objA;
  private B objB;
  private C objC;

Will, for instance, the "value" of A (new A()) be copied to objA after D's instantiation?

Garikai
  • 405
  • 2
  • 15
  • Related: [Is Java “pass-by-reference” or “pass-by-value”?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – jaco0646 Nov 30 '18 at 19:55

1 Answers1

1

Java only has references and primitive types. When you assign a reference to a variable it always takes the same amount if memory regardless of what object it references. Typically a reference is 4 bytes but can be 8 bytes for large heaps over 32 GB in size.

Will, for instance, the "value" of A (new A()) be copied to objA after D's instantiation?

In this case, the value of A is a reference to an object and that reference is copied. The object referenced isn't touched (neither copied or read)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130