Results 1 to 9 of 9
Like Tree2Likes
  • 2 Post By Junky

Thread: Getting the calling object in a constructor?

  1. #1
    Koala is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Getting the calling object in a constructor?

    Hi,

    I have a class (we'll call it ClassA) where one of the arguments of the constructor is Object. In another class (we'll call it ClassB) I am instantiating ClassA and passing (this) as the argument. That way ClassA will have access to all the attributes and methods of the calling object throughout the class. Is there a way I can get the calling object in the Constructor of ClassB without having to pass (this) to it? I've found several examples around the web that get the calling class but not the specific object.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Getting the calling object in a constructor?

    I'm not really sure what you are trying to do but I strongly suspect it is the wrong way to achieve what you want.
    Fubarable and Sierra like this.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Getting the calling object in a constructor?

    I agree with Junky. Rather then tell us what code you're trying to create, why not tell us what behavior you're trying to create. Quite possibly the best solution is one that is completely different from yours.

  4. #4
    Koala is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Getting the calling object in a constructor?

    I want the methods of the instantiated class to do different things based on the attributes of the object that instantiated it. Is that so wrong?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Getting the calling object in a constructor?

    How about instead using the Abstract Factory pattern to allow the calling code to request an instance of the new class that would fulfill its requirements. Or perhaps the dependency injection pattern.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Getting the calling object in a constructor?

    Java Code:
    class X {
        private String text;
        private Y y;
    
        X(String t) {
            text = t;
            y = new Y(this);
            y.doStuff();
        }
    
        public String getText() {
            return text;
        }
    }
    
    class Y {
        private X x;
    
        Y(X obj) {
            x = obj;
        }
    
        public void doStuff() {
            if(x.getText.equals("hello")) {
                // do this
            } else {
                // do that
            }
        }
    }
    Is that what you mean? If not I still have no idea what you want.

  7. #7
    Koala is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Getting the calling object in a constructor?

    Well to simplify the problem as much as possible it is more like this:

    Java Code:
    public class ClassA extends SomeSuperClass
    {
    private int someInt;
    
    public static void main(String[] args)
    {
    someInt = 42;
    ClassB myNewObject = new ClassB(this);
    myNewObject.doStuff();
    }
    
    public int getSomeInt()
    {
    return someInt;
    }
    }
    Java Code:
    public class ClassB
    {
    
    private SomeSuperClass caller;
    
    //Constructor
    public ClassB(SomeSuperClass caller)
    {
    this.caller = caller;
    }
    
    public void doStuff()
    {
    int theInt = caller.getSomeInt();
    // Do a bunch of stuff with theInt
    }
    }
    Sorry not sure why my tab key isn't working...

    Anyway my question is, instead of passing (this) to ClassB, is there a way to get the calling object in the constructor of ClassB, and be able to harvest the value of someInt out of it to use throughout ClassB?

    I know in this particular example it would just work to put the doStuff() method in ClassA but my actual program is more complicated and I need it to work this way.

    I also realize I can just pass someInt to the constructor directly but I don't want to do that either. I just want to be able to instantiate a new class without having to pass anything to the constructor, and have the constructor be able to get attributes of the calling object.
    Last edited by Koala; 10-31-2011 at 03:17 AM.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Getting the calling object in a constructor?

    Firstly your code has static/non-static issues. Secondly what is the problem of using "this" when creating another object? That is exactly what it is for.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Getting the calling object in a constructor?

    Tab doesn't work with the forums, use spaces, or type and format it in an editor, then copy/paste it here.

    I suggest you also look into the strategy pattern to see if it's what you are looking for (I am a bit confused as well): Strategy pattern - Wikipedia, the free encyclopedia

Similar Threads

  1. Create an object without calling the constructor?
    By sublixt in forum New To Java
    Replies: 7
    Last Post: 10-28-2011, 12:49 PM
  2. Calling a constructor from another one.
    By Somelauw in forum New To Java
    Replies: 1
    Last Post: 11-28-2009, 03:42 PM
  3. Constructor calling
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-22-2007, 06:53 PM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •