passing object of class to another class
Do something like this
class ClassA {
public ClassA() { // Constructor
ClassB b = new ClassB(this);
}
class ClassB {
public ClassB(ClassA a) {...}
}
The this keyword essentially refers to the object(class) it's in.
Yes, it will work. And it's a decent way to do it. You just pass an instance of class A:
public class Foo {
public void doFoo() {..} // that's the method you want to use
}
public class Bar {
private Foo foo;
public Bar(Foo foo) {
this.foo = foo;
}
public void doSomething() {
foo.doFoo(); // here you are using it.
}
}
And then you can have:
Foo foo = new Foo();
Bar bar = new Bar(foo);
bar.doSomething();