Reputation: 13
I've 3 classes in one package. The first class (ClassStart
) generates each an instance of the 2 other classes (ClassA
and ClassB
). I want to call in ClassB
a method of ClassA
by means of its instance "a".
Though the scope of Instance "a" is the package (because of the attribute "ClassA a;"
in ClassStart
the line "a.showText()"
doesn't work. It gets the error message "a cannot resolved"
.
So I tried "s.a.showText()"
but it doesn't work because the instance "s" was generated in a static method and I don't know how to access to "s".
The first class (contains the main-method):
public class ClassStart {
ClassA a;
public static void main(String[] args) {
ClassStart s = new ClassStart();
}
public ClassStart() {
a = new ClassA();
ClassB b = new ClassB();
}
}
The second class:
public class ClassA {
public void showText() {
System.out.println("This text comes from ClassA.");
}
}
The third class:
public class ClassB {
public ClassB() {
a.showText();
}
}
How can I call in ClassB the method "showText()" of the ClassA?
(I had looked for answers in this forum but I didn't find a answers for a three class problem like this.) Thank you for answer.
Upvotes: 1
Views: 2543
Reputation: 285405
If the ClassA object needs to be the same throughout, then pass it into B:
public class ClassB {
private ClassA a;
// pass the ClassA reference into the ClassB constructor
public ClassB(ClassA a) {
this.a = a; // assign it to the a field
// a.showText(); // or you can do this if you need it called in the constructor
}
// or do this if you want the a method called in a ClassB method.
public void callAShowText() {
a.showText();
}
}
then:
public class ClassStart {
ClassA a;
public static void main(String[] args) {
ClassStart s = new ClassStart();
}
public ClassStart() {
a = new ClassA(); // create your ClassA instance
ClassB b = new ClassB(a); // pass it into your ClassB instance
b.callAShowText();
}
}
The key bit of understanding here is to understand reference types and reference variables. You want a reference variable in ClassB to refer to the ClassA object created in ClassStart. One way to do that is to pass the object into ClassB either in its constructor or in a setter method. Once you've done that, then ClassB has the reference it needs and it can call any ClassA method on the instance.
Note that you can also "solve" this by creating and using a public static ClassA variable or a public static showText()
method, but in general you will try to avoid doing this since while it would work fine in a simple example like this, it doesn't "scale" well, meaning if used generally in larger more complex programs, it will risk increasing the potential connections in your complex program, greatly increasing the risk of bugs. It was for this reason, to decrease complexity and decrease connectedness (decrease coupling) that object oriented programming was created.
Upvotes: 2
Reputation: 291
Make the method static:
public static void showText()
Then call it:
ClassA.showText();
Upvotes: 2