Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java invoker

Name: Anonymous 2012-12-01 19:13

In Java, is it possible to get the invoker of a class? I am trying to create two classes simultaneously but I want them to reference each other, preferably upon creation(constructors) if possible. I know I can sort of do it by returning the stack trace, but this doesn't exactly seem... proper. Any ideas /prog/?

Name: Anonymous 2012-12-01 19:18

What in the Hell are you actually building, and why are you doing it in Java? I feel like you've either picked the wrong language, or are solving a problem in a non-straightforward way.

What do you mean by "reference each other"? Is there a property in each class that points to the other? So you have to reference an instantiated object in another object?

This seems like it's going way too far down the OOP rabbit hole.

Name: Anonymous 2012-12-01 19:21

Give more specifics about what exactly you are trying to do and I might be able to help. It might involve instantiating one class in the constructor of another, but I'm not sure that's what you want exactly.

Name: Anonymous 2012-12-01 19:25

Class1 {
   Class2 class2;
   public Class1(Class2 class2) {
      this.class2 = class2;
   }
   public static void main(String[] args) {
       Class1 class1 = new Class1(new Class2());
       //reference of class 2 from class 1 is complete.
       //but how can i reference the vice versa as well, so
         both objects reference each other?
   }
}

Class2 {
   public Class2() {
   }
}

Name: Anonymous 2012-12-01 19:25

>>4
Uh, what are you actually building?

Name: Anonymous 2012-12-01 19:26

Thanks for your interest guys, I hope that sheds some clarity. If I'm missing a completely obvious way please advise! But they must be two separate classes.

Name: Anonymous 2012-12-01 19:26

>>6
Is this homework?

Name: Anonymous 2012-12-01 19:27

>>5

A program implementing MVC, where Class1 is controller, and Class2 is view

Name: Anonymous 2012-12-01 19:28

>>7

No, but it was a previous coursework assignment which I have long completed. I just want to know whether or not I can access the invoker of an object - and preferably store it if possible. It's curiosity, and generally wanting to know more.

Name: Anonymous 2012-12-01 19:33

>>9
So you're assuming that you create an object inside of another, and want to store that.

Do you see how confused that is?

Take a step back, stop worrying about OOP and objects, and think about the logic of your program. You have two classes. They need to talk about each other. Does it really make sense for the MVC paradigm to link them all together from the start? No, it doesn't. That's missing the point.

Abstract your logic. Take it out of the class. The class is just a data structure, like any other.

Name: Anonymous 2012-12-01 19:38

>>10

I think that this is it. I mean, I know I can reference it later but I guess I was just being pedantic. Thanks for your reply though, much appreciated. Essentially, I want this:

A references B.
B references A.

Probably would have been easier to state, but I digress. Could I be a pain and ask you to explain a bit further? Mainly about the logic of the program, and missing the point. I mean I understand what your saying, but still somewhat hazy.

Name: Anonymous 2012-12-01 19:45

>>11
No problem.

I just don't want to see you [i]over[\i] apply the OOP paradigm to solving your problem.

It's not entirely your fault, because in Java, it's shoved down your throat.

First, without seeing your program, I'm trying to encourage a mindset. I want you to think of how you can use the tools to solve your problem, and not do everything your tools make you think you should do. People who don't are dumb and suck at programming. You don't seem dumb, so please, don't suck at programming.

Now, for the concrete part:

Right, at the low level, you want the Model to "know" about the instantiated View object, and vice versa. You also want to know if you can do that on creation, easily and automatically, right? I completely understand why you'd want to do that.

However, I think it's much better for solving the problem if you ignore cramming everything in the object. I'm saying the part where you want to two classes to correspond to each other, or know about each other at all, should be handled in logic inside of a function, and not be something intrinsic to the classes themselves. It's a lot cleaner.

You're not a pain, and if I'm still not clear, let me know.

Name: Anonymous 2012-12-01 19:45

>>12
Also, I suck at BBCode.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List