Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #11106

Best Way to Pass Info Between Objects?

From Novice <novice@example..com>
Newsgroups comp.lang.java.programmer
Subject Best Way to Pass Info Between Objects?
Date 2012-01-08 16:41 +0000
Organization Your Company
Message-ID <Xns9FD47717636DCjpnasty@94.75.214.39> (permalink)

Show all headers | View raw


Sorry, that's probably not the best of subject lines but I'm having trouble 
coming up with a concise one....

I'm trying to reason out the best way to pass information from an 
instantiating class to an instantiated class. So, let's say class Foo 
invokes class Bar to do something. Bar needs some specific information from 
Foo to do its job. What is the best way to pass this information from Foo 
to Bar?

For instance, Foo is a class that is used to edit a table of information 
presented as a JTable. Bar is a class that is used when a new row needs to 
be inserted into the table. The person using Foo right-clicks, chooses 
"Insert" on the context menu, and Bar gets instantiated to display a dialog 
with input fields so that the user can supply the values for a new row of 
the JTable. 

Bar needs various things to do its job. Among these are:
- the locale to be used since Bar can display its text in various languages 
- the logger to be used for error messages
- a reference to Foo since Bar wants to know the parent JFrame
- the title of the dialog

There are obviously various techniques for passing information from Foo to 
Bar. You can put the information in the paramater list. You can use getters 
to obtain the information from Foo. You can make values in Foo accessible 
to Bar by making the public so that Bar can access them directly. All of 
these methods can and do get used in Java. 

What are the rules of thumb to use in deciding which techniques to use in 
any given case? 

Clearly parameter passing is used widely but parameter lists tend to be 
short, mostly in the 0 to 4 parameter range. How do you decide which of the 
possibly many things needed by class Bar get passed as parameters and which 
get passed via getters or accessed directly from Foo?

My strong impression is that it's bad form to access variables directly in 
Foo and that getters are preferred, but please correct me if I'm wrong. But 
I still don't quite see when you prefer passing something in a parameter 
list rather than via a getter.

I'm not sure if there is a generally agreed-upon formula here or whether it 
is more a case of individual style and preference. I'd be very interested 
in your comments on this subject. Right now, I feel like I'm being pretty 
inconsistent in my techniques and would like to standardize them along the 
best possible lines.

-- 
Novice

Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Best Way to Pass Info Between Objects? Novice <novice@example..com> - 2012-01-08 16:41 +0000
  Re: Best Way to Pass Info Between Objects? Patricia Shanahan <pats@acm.org> - 2012-01-08 09:08 -0800
    Re: Best Way to Pass Info Between Objects? Novice <novice@example..com> - 2012-01-08 18:22 +0000
      Re: Best Way to Pass Info Between Objects? markspace <-@.> - 2012-01-08 10:45 -0800
      Re: Best Way to Pass Info Between Objects? Lew <noone@lewscanon.com> - 2012-01-08 11:49 -0800
      Re: Best Way to Pass Info Between Objects? Patricia Shanahan <pats@acm.org> - 2012-01-08 14:04 -0800
        Re: Best Way to Pass Info Between Objects? Martin Gregorie <martin@address-in-sig.invalid> - 2012-01-08 22:40 +0000
          Re: Best Way to Pass Info Between Objects? Patricia Shanahan <pats@acm.org> - 2012-01-08 15:56 -0800
            Re: Best Way to Pass Info Between Objects? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-09 06:34 -0400
              Re: Best Way to Pass Info Between Objects? Jeff Higgins <jeff@invalid.invalid> - 2012-01-09 12:39 -0500
                Re: Best Way to Pass Info Between Objects? Jeff Higgins <jeff@invalid.invalid> - 2012-01-09 12:42 -0500
                Re: Best Way to Pass Info Between Objects? Lew <lewbloch@gmail.com> - 2012-01-09 13:43 -0800
                Re: Best Way to Pass Info Between Objects? Jeff Higgins <jeff@invalid.invalid> - 2012-01-09 17:26 -0500
                Re: Best Way to Pass Info Between Objects? Lew <noone@lewscanon.com> - 2012-01-10 06:47 -0800
                Re: Best Way to Pass Info Between Objects? Patricia Shanahan <pats@acm.org> - 2012-01-09 14:26 -0800
                Re: Best Way to Pass Info Between Objects? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-09 19:06 -0400
                Re: Best Way to Pass Info Between Objects? Jeff Higgins <jeff@invalid.invalid> - 2012-01-09 18:46 -0500
                Re: Best Way to Pass Info Between Objects? Jeff Higgins <jeff@invalid.invalid> - 2012-01-09 19:52 -0500
                Re: Best Way to Pass Info Between Objects? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-10 06:43 -0400
              Re: Best Way to Pass Info Between Objects? Martin Gregorie <martin@address-in-sig.invalid> - 2012-01-10 02:11 +0000
              Re: Best Way to Pass Info Between Objects? Gene Wirchenko <genew@ocis.net> - 2012-01-09 20:17 -0800
              Re: Best Way to Pass Info Between Objects? Lew <noone@lewscanon.com> - 2012-01-10 06:43 -0800
                Re: Best Way to Pass Info Between Objects? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-10 19:13 -0400
      Re: Best Way to Pass Info Between Objects? Jeff Higgins <jeff@invalid.invalid> - 2012-01-09 14:30 -0500
        Re: Best Way to Pass Info Between Objects? Jeff Higgins <jeff@invalid.invalid> - 2012-01-09 17:28 -0500
          Re: Best Way to Pass Info Between Objects? Martin Gregorie <martin@address-in-sig.invalid> - 2012-01-10 02:24 +0000
            Re: Best Way to Pass Info Between Objects? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-10 06:35 -0400
              Re: Best Way to Pass Info Between Objects? Martin Gregorie <martin@address-in-sig.invalid> - 2012-01-10 22:48 +0000
  Re: Best Way to Pass Info Between Objects? v_borchert@despammed.com (Volker Borchert) - 2012-01-08 17:17 +0000
  Re: Best Way to Pass Info Between Objects? Jeff Higgins <jeff@invalid.invalid> - 2012-01-08 13:43 -0500
    Re: Best Way to Pass Info Between Objects? Jeff Higgins <jeff@invalid.invalid> - 2012-01-08 19:01 -0500
  Re: Best Way to Pass Info Between Objects? Jeff Higgins <jeff@invalid.invalid> - 2012-01-08 14:10 -0500
    Re: Best Way to Pass Info Between Objects? Jeff Higgins <jeff@invalid.invalid> - 2012-01-08 14:41 -0500
  Re: Best Way to Pass Info Between Objects? Roedy Green <see_website@mindprod.com.invalid> - 2012-01-09 04:13 -0800

csiph-web