简体   繁体   中英

As java doesn't have referencing, how does one pass information between two separate classes?

Sorry for the extremely vague/confusing, I really didn't know how to name this issue. If someone has a better one please feel free too edit it. Onto my issue,

public class Foo()
{
    String name;
    public Foo()
    {
        Bar temp = new Bar();
    }
}    

public class Bar()
{
    public Bar()
    {
        setFooName("newName");
    }

    public void setFooName(String name)
    {   
        // Is it possible call a method in Foo?
    }
}    


public class test {
    static public void main(String[] args) 
    {
        Foo foobar = new Foo();
    }
}    

I'm still new to java so I'm not sure if this is even possible, but using C++ I'd normally have a local variable of the 'Foo' inside the 'Boo' class and then pass a reference to the object into the constructor of Bar and then assign it to the local variable in Bar.

As java doesn't have referencing I'm stuck on the matter. The reason I want to do this is the example I have is I need to create a GUI object inside of a class and then have information from the GUI object sent back to the class it was created in.

I do hope this all makes sense, if it doesn't, sorry.

I think you're getting caught up on symantics.

The following example passes a copy of the reference of Foo to Bar via Bar 's constructor. This now allows Bar to access properties of the instance of Foo . What it doesn't allow you to do is change the reference between the two (ie assigning a new reference of Foo within bar will not change the previous reference)

public class Foo()
{
    String name;
    public Foo()
    {
        Bar temp = new Bar(this);
    }
}    

public class Bar()
{
    private Foo foo;
    public Bar(Foo foo)
    {
        this.foo = foo;
        setFooName("newName");
    }

    public void setFooName(String name)
    {   
        // Is it possible call a method in Foo?
    }
}    


public class test {
    static public void main(String[] args) 
    {
        Foo foobar = new Foo();
    }
}  

Bar could then call any modifiers that Foo provides and it would change those individual properties of Foo for everybody who had a reference to that instance...

I hope that's close to what you're asking :P

Actually you can do this in Java. Java has references, but not pointers. Confusingly, for C++ aficionados, these references can be null.

This means you can certainly do the following:

public class Bar {
    private Foo foo;
    public Bar(Foo foo) {
        this.foo = foo;
    }
    public setFooName(String name) {
        foo.setName(name);
    }
}
public class Foo {
    private String name;
    public Foo() {
        Bar temp = new Bar(this);
        temp.setFooName("fooname");
    }
}

But I'm not sure, in this instance, why you wouldn't just write:

public class Foo {
    private String name;
    public Foo() {
        this.name = "fooname";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM