Is there a way to set the instance variables of one class to the instance variables of another class, and when the instance variables of the second class changes, the instance variables of the first class also change along with it BEFORE making objects out of either classes? This is my Dog class:
public class Dog {
int size;
Dog(int size) {
this.size = size;
}
public static void main(String args[]) {
Cat cat = new Cat();
Dog dog = new Dog(cat.size);
System.out.println(dog.size);
cat.size = 17;
dog.size = cat.size;
System.out.println(dog.size);
}
}
This is my Cat class:
public class Cat {
int size = 5;
}
As you can see, I have to make objects out of both of them to set dog.size to cat.size. Is there a way to make it so that before you make the objects, the instance variable 'size' in the Dog class automatically gets set to the instance variable 'size' in the Cat class? Basically, if the instance variable 'size' in the Cat class gets set to 20, I want the instance variable 'size' of every object I make out of Dog class to also get set to 20. I hope my explanation wasn't too confusing. Oh, and also, I know you can do this with inheritance, but the class I'm actually using is already inheriting another class, so I can't use that method. If anyone know any other methods, please let me know. Thank you.