Reputation: 226
I have two classes A
and B
. in class A
i create an object of B
.
From within the object x, I would like to access values of datamembers one
and two
.
Is the only solution for that, passing it as parameter like
b=new B(one,two)
?. I donot want to extend, because class A
is a frame and class B
is a panel.
class A
{
private int one;
private String two;
private myMethod()
{
B x=new B();
}
}
Upvotes: 0
Views: 62
Reputation: 13187
There are several ways you can go (I'll discuss each below)
Ad 1. This is the easiest approach, simply use a constructor like
public B(int one, String two) {
...
}
As you already mentioned, this is cumbersome if the number of parameters is large.
Ad 2. This is not a good idea. Does the panel really need to have access to all the properties of the frame? No, it doesn't. For instance you could:
public B(MyFrame a) {
...
a.setVisible(false);
}
which is absolutely undesired. Besides, the problem with circular references is that you cannot make changes in isolation: changes to your frame can cause changes in the panel and vise versa.
Ad 3. This would be my preferred approach. You create an interface that provides exactly the functionality you need:
public interface MyInterface {
public int getOne();
public String getTwo();
}
Then you let your class A
implement that interface:
public class A implements MyInterface {
...
public int getOne() {
return one;
}
public String getTwo() {
return two;
}
}
You know change the constructor of B
to
public B(MyInterface a) {
// use a.getOne() and a.getTwo() to get your data
}
And you can still create B
from A
as
B b = new B(this);
The three main advantages are:
A
than neededB
does not explicitly depend on class A
(only on interface MyInterface
)Upvotes: 1
Reputation: 4826
If classes A and B are separate components than I am afraid you don't have another choice.
If on the other hand class B belongs to class A in a functionality-wise manner you could make class B an inner class of A:
public class A {
int one;
class B {
private void doSomething() {
one = one + 1; // inside B you can access memebers of A
}
}
}
Upvotes: 1
Reputation: 17923
Another way would be as follows:
class B
{
A a;
public B(A a){
this.a = a;
}
}
This will require that you have public accessor methods for the attributes of class A
Upvotes: 0
Reputation: 8483
You can delcare like this
class B
{
private int one;
private String two;
// getter setters methods
}
and call form Class A 's myMethod()
should like thid
private myMethod()
{
A a = new A();
B b = new B();
b.setOne(A.one);
b.setTwo(A.two);
}
Upvotes: 0
Reputation: 2865
If you want one and two to be accessible outside of the class you can -
Then you can pass an instance of a (for example by using the this keyword) to the constructor of b;
Upvotes: 1
Reputation: 35587
class B
{
private int one;
private String two;
public B(int one,String two) //constructor of B
{
this.one=one;
this.two=two;
}
// getter setters
}
If you create class B as above you can call B b=new B(one, two);
Upvotes: 1