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

Pages: 1-

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.

Name: Anonymous 2012-12-01 19:51

Java is shit, design patterns are shit, >>1-13 are shit.

Name: Anonymous 2012-12-01 19:53

>>12

That's really helpful. So perhaps, instead of worrying about the constructors so much(Looking back, I'm not sure why I was "obsessed" with them doing all the referencing, most likely due to it being automatic as you mentioned before), simply use functions that handles the logic of referencing them instead, probably calling them within the main().

Name: Anonymous 2012-12-01 19:55

>>15
Yeah. Keep talking about the other class out of the class itself. It sort of breaks that abstraction.

This is why a lot of people hate Java. Is Java your first language?

Name: Anonymous 2012-12-01 19:58


class Controller {
    private View _view;

    public Controller(ViewFactory vf) {
        _view = vf.build(this);
    }

    public getView() {
        return _view;
    }
}

class View {
    private Controller _controller;

    public View(Controller controller) {
        _controller = controller;
    }

    public getController() {
        return _controller;
    }
}

class ViewFactory {
    private String name;

    public ViewFactory(String name) {
        _name = name;
    }

    public View build(Controller controller) {
        // use reflection, not sure if this is right
        return Class.forName(name).newInstance(controller);
    }
}

// usage
ViewFactory vf = new ViewFactory("View");
Controller c = new Controller(vf);
View v = c.getView();


the other way to build a circular reference is to use setter injection

Name: Anonymous 2012-12-01 20:04

>>16

I would have to say so, along with C (but the program complexity varied wildly between the two when we were given assignments, so much more Java). My university is focused on OOP as you can most likely tell, and functional programming - but that's another story. You seem very experienced with programming, my guess is you may be a tutor of sorts as your explanations were well-rounded? But hey, I could be mistaken heh.

I would also like to say that I really appreciate your help tonight, I also managed to search engine the invoker question, but it turns out I was asking the wrong question as I was getting far to caught up in the OOP concept(s).

Name: Anonymous 2012-12-01 20:13

>>17

Wow, thank you for taking the time to help me with this. Mind if I c&p so I can analyze it a bit later (I do have a life, honest!)

Name: sage 2012-12-01 20:59

>>14
Shut up you worthless fuck. Your incessant complaining about how everything isn't LISP isn't even worth my anger.

Name: Anonymous 2012-12-01 21:07

>>20
Why even bother replying?

I wasn't suggesting the use of Lisp. Yes, Common Lisp is far superior to Java in terms of OOP, but that doesn't have anything to do with this. You can do useful applications without having to use retarded things like this. Either way, don't you have some abstract interface abstractions to abstract?

By the way, cool use of sage as an insult, /g/tard.

Name: Anonymous 2012-12-01 22:01

>>20
If it ain't Lisp, it's crap.

Name: Anonymous 2012-12-01 23:21

>>22
If it ain't Dubs, it's shit.

Name: Anonymous 2012-12-02 0:18

>>18
Hey, no problem, and no, I'm not a tutor (well, not for CS anyway; I do tutor some mathematics).

I'm a guy who taught himself a lot of functional programming (I actually did read SICP on my own) and low-level stuff. I enjoy everything from reverse engineering to writing random stuff in Common Lisp, just because it's fun.

Unfortunately, I took a 6 month stint at an office programming Java and SQL to see what it was like, and to pay my bills. Never again.

So, I try to save others from becoming what I had to deal with at that job (someone actually wrote an authentication system in Javascript, which was bypassed by, of course, disabling Javascript...that's what I had to deal with).

I'm more than happy to help, but I mostly read this board to laugh at the satire of Reddit, Hacker News, Rails fanboys and other self-righteous shitheads that make the programming world shit.

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