0

The problem is that i got 2 classes. The first class has a method which created an object of the second class. Is it possible to get that object without having a return statement? And also not making it a field

Example:

//first class

public class First {

    public First() {
    }

    public void aMethod(Object o) {
        Second s = new Second(o);
    }
}

//second class

public class Second {

    public Second() {
    }
}

The method is called via a click of a button.

EDIT: Sorry for the confusions. But the reason i dont want to change the code is because I'm making a JUnit test.

The two classes are actually jFrames and I'm trying to test the buttons. In the first class the method is called via a button. In the method it creates the second Jframe. In this Jframe i also want to test a button, thats why I need the instantiated jFrame object. In the aMethod() im also passing some paramenter, which the second JFrame needs. So I cant test It individually.

  • 2
    can `aMethod()` have parameter(s)? – Kent Mar 14 '13 at 15:11
  • The question you've asked doesn't really make sense - why would you not want to return `s` if you want to use it outside `First`? Please can you edit your question to explain **what** you want to achieve, not **how** you think you should achieve it? – RB. Mar 14 '13 at 15:11
  • May be this can help:- http://stackoverflow.com/questions/2408626/returning-a-void-object – Rahul Tripathi Mar 14 '13 at 15:13
  • no return, no field(so static or non-static variable right?) then I guess the only way I can think of is reflection which I don't think is the right way... but I don't think that's really what you want... – cwhsu Mar 14 '13 at 15:13
  • 1
    @cwhsu You could do it by mutating a parameter passed into the method (basically a class which is just a wrapper for `Second`). I think this is what Kent is hinting at. However - I think the requirement makes no sense, hence why I feel the author should clarify his intent. – RB. Mar 14 '13 at 15:14

4 Answers4

1

It all depends on how thread safe your First object is/needs to be.

You could set an instance variable and then expose a getter but maybe you are trying to work inside some other constraints as this seems a pretty weird alternative to just returning the value.

public class First {
    private Second s;
    public First() {
    }

    public void aMethod() {
         s = new Second();
    }

   public Second getSecond(){
        return s;
   }
}

EDIT: Another answer, given I missed the no field constraint somehow.

You could use Observable pattern so that you client Object registers as a listener to First and then you can do;

Second s = new Second();
setChanged();
notifyObservers(s);

http://docs.oracle.com/javase/6/docs/api/java/util/Observable.html

Again this seems like overkill, maybe you could give more context as to the reason you can't edit the method signature or use fields.

James
  • 1,541
  • 12
  • 25
1

There are two possibilities I could think of:

1) add parameter in aMethod() for example List list (or other types has Second Field), then you could list.add(new Second()) the caller could get it from the list (last element)

2) another class (if you have), has a static field private static Second second, and you have to write setter/getter, also care about the thread safe problem... then you can in aMethod() Foo.setSecond(new Second()); really bad in practice.

in the end, I would ask, do you really need do this??

Kent
  • 189,393
  • 32
  • 233
  • 301
0

You can also return it inside a parmeter you pass into the method call. something like:

    public void aMethod(Result result) {
             s = new Second();
             result.val = s;
        }

where Result might be a class having a member of type Second. or maybe a generic class :

Result<Second>

But i'm not sure how that would be better than a simple return.

omer schleifer
  • 3,897
  • 5
  • 31
  • 42
0

The most common was is use so called Factory Pattern

public class First {

public First() {
}

public Second aMethod(Object o) {
    return new Second(o);
}

}

user8426627
  • 903
  • 1
  • 9
  • 19