I’m new to OOP and I was wondering how to set something that is not like int, string, double etc.
I have two classes, Foo and Bar, and some instance variables
How can I set the Bar type instance variables?
public class Foo
{
//instance variables
private String name;
Bar test1, test2;
//default constructor
public Foo()
{
name = "";
//how would I set test1 and test 2?
}
}
public class Bar
{
private String nameTest;
//constructors
public Bar()
{
nameTest = "";
}
}
Same way as anything else, create an instance of a
Barand set it on the instance property.You can create those instances in a constructor, pass them to a constructor, or set with a setter: