So I have this scenario
class A:
public class A {
B b = new B();
public void doSomething() {
// this does something
}
}
and class B:
public class B {
public B() {
// need to access A
}
}
The problem is that I need to access the instance of A from the A's instance variable B. How do I do that?
You may simply pass the instance of A to B:
class A {
B b;
A() {
b = new B(this);//<-- A's instance
}
}
class B {
A a;
B(A a) {
this.a = a;
}
}
If class Bneeds to access an existing instance of A then you must provide the B instance with a reference to the A instance.
class B {
private A a;
B(A a) {
this.a = a;
// do something with a
}
}
then
A a = new A();
B b = new B(a);
If you need access to the A only within the constructor then you don't need to store a local copy of the reference.
You must create new object from instance A
A aa = new A();
OR
public class B extends A {
public B() {
// need to access A
}
}
Inner Class seems like the best thing to do ,
public class A {
B b = new B();
private int tryToAccess;
public void doSomething() {
// this does something
}
public class B{
public B()
{
tryToAccess=5;
}
}//B ends
}//A ends
Note:This is just one way of going about it , here you can access A's private variables from class B
Related
This question already has answers here:
calling a function in a class's "owner" class
(8 answers)
Is there a way to access the variables of the calling class in a method?
(3 answers)
Closed 3 years ago.
An instance a of class A has an instance b of class B.
How can b access a variable of a?
class A {
boolean flag;
B b;
public static void main(String[] args) {
A a = new A();
}
public A() {
b = new B();
b.doSomething();
chageFlag();
b.doSomething();
}
void changeFlag() {
// do something with flag
}
// other stuff
}
class B {
void doSomething() {
// here I need to access a from the instance owning b.
boolean aFlag = ?? // how to access a.flag ??
}
}
You will not be able to access a variable of A in this instance because A and B have no parent/ child or outer/inner class relationship here.
The way to do this is to pass the instance of A to B, such as,
B b = new B(this);
For this you need to adjust the constructor to take in A as a parameter.
As your code is, b has no way of reaching a; simply because it does not have a reference to it.
If the two classes are so related that you need b to know about a, then you can make B an inner class in A:
class A {
boolean flag;
B b;
public static void main(String[] args) {
A a = new A();
}
public A() {
b = this.new B();
b.doSomething();
changeFlag();
b.doSomething();
}
void changeFlag() {
}
class B {
void doSomething() {
boolean aFlag = flag;
}
}
}
B.doSomething() is able to read flag from the enclosing class because B is an inner class.
Be aware that this solution is not appropriate in all situations, it makes the coupling between the two classes even tighter.
Hi this is a basic question, but kindly bear with me.
I have two classes and on class has a reference of another class. How can i create the instance of second class which is present in first class at the time creation of instance of first class. Are any utility present for this.
Code ::
class A {
B b;
}
class B {
int member;
}
In a Contructor (like Robert Kock already said)
class A {
B b;
public A(){
b = new B();
b.member = 5;
}
}
Directly as Attribute
class A {
B b = new B(5);
}
With Initializer
class A {
B b;
{
b = new B();
b.member = 5;
}
}
Within the constructor of the first class:
class A
{
public A()
{
b = new B();
}
B b;
}
Or even like this:
class A
{
public A()
{
}
B b = new B();
}
A general solution would be:
public class A {
private final B b;
public A(B b) {
this.b = b;
}
}
...
A a = new A(new B());
It becomes interesting when both instances refer to each other, then you need to use a setter in at least one of the classes:
public class B {
private A a; // the field cannot be final in this case
public void setA(A a) {
this.a = a;
}
}
....
B b = new B()
A a = new A(b);
b.setA(a);
The answers where the class is creating the other instance itself are not a general solution.
JAVA
public class A {
string amountA;
B b;
}
public class B {
string amountB;
public void setValue(String value) {
amountB = value;
Also, I need to set amountA = value;. Is it possible? See main method
}
}
... main(String... args) {
A a = new A();
B b = a.getB(); // b is a member of A
b.setValue("25") // this should also change 'amountA' in object 'a' to '25'
}
If you need to set both valueA and ValueB to the same value, it makes more sense to have a setter in class A that would set both of them :
public void setValue(String value) {
amountA = value;
b.setValue(value);
}
You can't access an instance of A from within an instance of B, since B is not an inner class of A. You can create an instance of B that is not related at all to any instance of A.
public class A {
public class B {
string amountB;
public void setValue(String value) {
amountB = value;
amountA = value; // Using A.this.amountA
}
}
string amountA;
public B createB() {
return new B(); // Provides A.this to the B instance.
}
}
... main(String... args) {
A a = new A();
B b = a.createB(); // b is created inside A
b.setValue("25") // this should also change 'amountA' in object 'a' to '25'
}
You can use an inner class.
The class itself should better create the instance, so that A.this is set into the b.
Alternatively
B b = a.new B();
But I never ever used that.
Inner classes are practical for a container element having access to its container class.
Another solution is to make a method in A with the B object as parameter or so.
It sounds like you want to have A derive from B rather than contain an instance of B. That way the valueA in an instance of A and the valueA in the instance of B associated with that instance of A are really the same variable. You can also have data in A that isn't shared with B by declaring it in A. For example,
public class A extends B {
public String amountAonly; // a string in A that's not in B
public B getB() { return (B)this; } // typecast to treat A like B
}
public class B {
public String amountA; // a string that's in both A and B
public void setValue(String value) {
amountA = value;
}
}
...
main(String[] args) {
A a = new A();
B b = a.getB(); // b is the B associated with a
b.setValue("25"); // will also change 'amountA' in object 'a' to '25'
}
Code:
public class A{
B b = new B();
public class B{
public void fun(){ send(A); }
}
I want to do something with all A object in B.
I can create method in A class:
private A getThis(){return this;}
But is it other solution (some keyword)?
Try this code inside your inner class.
A.this
It should give you a reference to the enclosing instance from the outer class.
Here is a small example.
public class A {
private B b = new B();
public class B {
public void fun() {
}
public A getEnclosing(){
return A.this;
}
}
public static void main(String[] args){
A a = new A();
System.out.println(a == a.b.getEnclosing());
}
}
Try
B b = new B(this);
Then B contructor
public B(A a) {
this.a = a;
}
I have a function in Class A which I would like to change the value of a field in Class B.
Class C has my main() and creates a new instance of class B and Class A. Class A is from an API and one of their functions is a listener function. I would like for that listener function to be able to change the field of Class B, but when I write the code for the listener function, it doesn't recognize Class B's instance.
How do I reference that instance?
Example code:
public class A {
public void listenermethod(){
//can't reference Binstance <-------
}
}
public class B {
B.field = 1;
}
public class C {
A Ainstance = new A();
B Binstance = new B();
}
You should give A class a private B field, and then you can call the public methods from B on this field as needed. If you need to create both A and B instances in a separate class (C) you should give your A class a public void setB(B b) setter method.
A.java
class A {
private B b;
public void setB(B b) {
this.b = b;
}
public void listenerMethod() {
if (b != null) {
b.someBMethod();
}
}
}
C.java
public class C {
public static void main(String[] args) {
A a = new A();
B b = new B();
a.setB(b);
a.listenerMethod();
}
}
You have to be able to modify both class C and class A. Rewrite the class A method to
public void listenermethod(Binstance theB){
theB.something = "some_value";
}
Now when you call class A, pass in the Binstance. If you can't modify class A, then your task can't be done.
An instance by definition belongs to an object. Therefore, your class A must either have an object of class B as a member:
Class A{
private B instance_of_b;
}
now you can access B members like this:
instance_of_b.member
or the field belonging to class B could be static and then A could access it through the class.
B.member
Also make sure you know the meaning of accessor keywords (private,protected,[friendly],public).