0

I'm wondering whether is possible to access to the information contained in a field of a class from another class that's been created inside of the first one. I put a snippit of Java code for showing I want to do.

    public class A {
      public String c = new String();
      B b;
      ...
      ...
      ...
      public void doSomething() {

        b = new B();
      }
    }

    public class B {        

      ...
      ...
      ...
      public void retrieveInformationFromA() {

        // I need to retrieve the field "c" of A instance that's 
        // created the B instance 
      }
    }

NOTE: Due restrictions of the current design of this code I can't to create a constructor in B that includes a parameter for field "c" of A class. Due to legacy code, I have to avoid changing the existing code as much as possible.

I appreciated any help!!

UPDATE: I've corrected the code, I forgot to add the public modifier to the field "c" in A.

mol
  • 153
  • 3
  • 12
  • Use a static instance for c? – zenbeni Nov 08 '13 at 09:50
  • Why can't you create another constructor for `B`? If you can't do this, then there's no way of `B` knowing anything about the `A` which created it; so you're not going to be able to do what you want. – Dawood ibn Kareem Nov 08 '13 at 09:51
  • It is possible if You define class B as a private inner class of class A – Arsen Alexanyan Nov 08 '13 at 09:51
  • or! why don't you try it out? That will stick a lot longer than having someone give you a microwave answer – Wim Ombelets Nov 08 '13 at 09:52
  • Why can't you use a Setter in B like: public class A{ public void dosmth(){ B b = new B(); b.passA(this); } } public class B { public void passA(Class a){ ... } } – worcin Nov 08 '13 at 09:53
  • If youre not allowed to make your code do what it should do, you should rethink the rules that do not allow it – LionC Nov 08 '13 at 09:54
  • @zenbeni: No, it can't be a static instance. – mol Nov 08 '13 at 09:56
  • 1
    You should edit your post and describe what you can change in this code (or what you can't change) it is too open. If you can change the method signature, add 'A' as a parameter for instance, but I feel that you can't do that because of legacy code. – zenbeni Nov 08 '13 at 10:05
  • @zenbeni you're right. I have to try to modify minimal code. I think the same as you (I haven't found any solution that allows to do that without change code of these classes), but I'd want to ensure that there isn't an alternative that allows it. Thanks!! – mol Nov 08 '13 at 10:27
  • 1
    @mol Well you can do evil stuff too, use a ThreadLocal to put content from A in the current thread, then get it from B, but it is overdesign, dangerous and won't work in multithread environment. – zenbeni Nov 08 '13 at 10:39
  • Ok, I see. That's not a good idea. I'll have to modify the code. Thanks!! – mol Nov 08 '13 at 10:44

5 Answers5

1

Since you did not specify any visibility (public, private, protected) to field 'c' it is implied to be "package protected" i.e. all classes in the same package as class A can access that field directly.

So, if your classes A and B are in the same package, you can access field 'c' directly from class B.

If they are not in the same package, you can not access it normally, but need to use reflection (and this should be the absolute last resort): How do I read a private field in Java?

Edit: But you still have to pass a reference to the instance of class A to the class B. If you can not change class B you are completely out of luck.

Community
  • 1
  • 1
Torben
  • 3,805
  • 26
  • 31
1

Declare the string as static

public class A {
  static String c = new String();

and then access it in B like

  public void retrieveInformationFromA() {

     String info = A.c;

    // I need to retrieve the field "c" of A instance that's 
    // created the B instance 
  }

If c needs to be different or non static

  public void retrieveInformationFromA() {


     A obj = new A();
     String info = obj.c;

    // I need to retrieve the field "c" of A instance that's 
    // created the B instance 
  }
Ankit Rustagi
  • 5,539
  • 12
  • 39
  • 70
1

In my experience that is not possible. The called instance b has no knowledge of the world surrounding it apart from what you hand down. You may include a setter with class B to hand 'c' down if you do not want or cannot declare c as static.

Another method could be an intermediate "InfoBase" class, which contains fields one or more different classes will need.

Christian Kullmann
  • 558
  • 1
  • 7
  • 21
1

You cant retrieve the field "c" of A because its private member of class A. To access field in this case use getter setters(for c in this case) or make it public ( a really bad approach). Happy Coding:)

Ali Kazmi
  • 1,460
  • 9
  • 22
1

You could do something like this

// In class A
public void doSomething() {
    b = new B();
    b.retrieveInformationFromA(this);
}
...
// In class B
public void retrieveInformationFromA(A a) {
    String c = a.c; // This way you can get it
    // I need to retrieve the field "c" of A instance that's 
    // created the B instance 
}
Rahul
  • 44,383
  • 11
  • 84
  • 103