public class object1{
public object2 myobj;
object1(){
myobj = new object2();
}
public object2 getMyobj(){
return myobj;
}
}
public class object2{
object2(){}
public void sayHello(){
System.out.println("Hello");
}
}
objects1 obj;
obj.myobj.sayHello();
obj.getMyobj().sayHello();
Console Result:
Hello
Hello