I am a beginner in java. I recently came across this situation.
Suppose we have a class C.
public class C {
C(A a, B b) {
this.a = a;
this.b = b;
}
A a;
B b;
....
}
Where a, b are objects of different classes.
public class A {
....
}
public class B {
....
}
So how do I create an object of type C ?
CodePudding user response:
You will need an instance of A and B to pass into the constructor of C. Something like this, assuming A and B have empty constructors:
A aObj = new A();
B bObj = new B();
C cObj = new C(aObj, bObj);
The cObj object's a and b fields will be references to the aObj and bObj instances that are constructed and passed in.
