Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

passing variables

  Asked By: Lewis    Date: May 05    Category: Java    Views: 1027
  

what is the best way to pass many variables from one class to another? they are
strings, if that has any relevence. thank you.

Share: 

 

10 Answers Found

 
Answer #1    Answered By: Jackson Bouchard     Answered On: May 05

The correct OO way to do this is to use the
Bridge Design Pattern.<br><br>I will describe the design
pattern and then explain WHY this is the best OO way to
do this.<br><br>You create a class  with a method
that accepts the two objects as parameters. Each class
should have get/set methods for each of the variables.
The bridge class would get the variables  one by one
from object A and pass  it to Object B.<br><br>This
sounds like a lot of work, but what this does is
"uncouple" Class A and Class B. Class A doesn't even have to
know about the existance of Class B and vice versa.
The "bridge" class is the object that
knows.<br><br>That way, if Class A changes, Class B doesn't have to
change. If class B goes away, class A doesn't have to
change. And the bridge only passes those pieces of
information that are needed to be passed. One could also have
Class A and Class B implement an interface which has
those methods describing the variables which need to be
passed.<br><br>While this is not the most efficient method, it is one
that helps in the maintaince, "reusablitiy" and
encapsulation of each class.<br><br>Hope this
helps.<br><br>Stephen McConnell<br>http://www.crosslogic.com

 
Answer #2    Answered By: Canan Kaya     Answered On: May 05

i realize that using objects is the best way, but
i have a program that at the moment uses no objects
at all, and is written more like a traditional C
program, is there an easy way using a method or two just
to pass  the variables?

 
Answer #3    Answered By: Isabelle Brown     Answered On: May 05

If you are programming in Java, you do not have
"a program that at the moment uses no objects at
all"! A class  definition in Java becomes an Object upon
instantiation. Even the application module with the main()
method is an object, once you run it. The best
way to pass variables  is to write a method that
returns the variable: public String getVariableX()
{return X;} where X is the string you are
returning. You could write a method that returns all strings in
an array: public String[] getVariables() {//build
array and return it} a third way is to simply
return the Object and let the calling code extract the
individual object variables. public SourceObj
getObject(){return sourceObj} where sourceObj is an instance of
SourceObj. You then extract the variables
directly: this.variable = sourceObj.variable; Many object oriented
folks don't approve of this final method because it
breaks the encapsulation philosophy of an
object. Here is some test code that shows 2 of the 3 ideas (I
used a long instead of a String variable). public
class TestObj { long end =
System.currentTimeMillis()+5, med = System.currentTimeMillis()+3, start
=
System.currentTimeMillis(); public TestObj (SourceObj sourceObj) {
System.out.println("Before"); System.out.println("start:"+this.start);
System.out.println("med:"+this.med);
System.out.println("end:"+this.end);
this.end=sourceObj.end; this.med=sourceObj.med;
this.start=sourceObj.start; System.out.println("After");
System.out.println("start:"+this.start);
System.out.println("med:"+this.med);
System.out.println("end:"+this.end); } public static void main (String[]
args)
{ SourceObj so = new SourceObj(); TestObj
newObj = new TestObj(so); System.out.println("source
start:"+so.getStart() );
System.out.println("SourceObj:"+so.getSourceObj().toString() );
} } class SourceObj { long end =
99,med = 50,start = 1; public SourceObj() {}
public long getStart() {return start;} public
SourceObj getSourceObj() {return this;} public String
toString() { return "SourceObj start:" + this.start + "
med:" + this.med + " end:" + this.end;
} } There are other ways to do it, but these just popped to
the top of my head.

 
Answer #4    Answered By: Teresa Rogers     Answered On: May 05

but i just can't seem to get this to work. how can you
pass a String array from one class  to another, and could you write the code out
for me...

 
Answer #5    Answered By: Hilma Miller     Answered On: May 05

Java is an Object Oriented Language. If you are not going to program in an
Object Oriented fashion, write in C.(I got up on the wrong side of the
bed today).

 
Answer #6    Answered By: Helga Miller     Answered On: May 05

What else do you want? a good massage,
maybe??<br><br>Sorry, but I think you've received enough help to do it
yourself now. You've been given good, bad and easy ways to
develop what you need.<br><br>It looks like a homework
for me, so I think you have to take the answers
you've got, analyze them for a while and just work a
couple hours on what you should be
doing...<br><br>(sorry, think I also got up on the wrong side of the bed
today)

 
Answer #7    Answered By: Willard Washington     Answered On: May 05

First of all, what are you passing  the String
array to? What is the target object (or class?) Do you
have any code that you can share which shows where
your problem is?

 
Answer #8    Answered By: Alan Palmer     Answered On: May 05

i know its been a while, but i still haven't
solved the problem, thank you to all who have stuck with
me here. this is the first java applet I've ever
written, and I am more familiar with C and similar
language structure (non OOP). Anyway, again, my problem is
as follows:<br>I have an applet doing calculations
from input, and then it is going to display a table
(in a popup window). So, I need a way to get the
calculated data (in one class) to the table (in another
class). I have tried using what i know about methods and
the "return" keyword, but my compiler keeps giving me
the error "Can't make static reference to method".
Anyway, I have no code to share, just because I don't
know how to go about doing it. If someone could give
me a clear example, I would be most
appreciative

 
Answer #9    Answered By: Guadalupe Rogers     Answered On: May 05

public Main{ public static void
main(String[] args){ String[] data =
{"value1","value2"}; Table t = new
Table(); t.setData(data); t.printData(); }}pu\
blic class  Table{ public void
setData(String[] data){ this.data =
data; } public void printData(){ for(int i = 0; i <
data.length; i++){ System.out.println("Data " + i +
"=" + data[i]); } } private
String[] data;}That is probably as
simple of an example that I can give. It is not an
applet, but rather an application, although in the big
picture that shouldn't matter. Notice that I create an
instance of my Table class and then call the methods on
that instance.If you continue to have problems
then you are going to have to produce some code for
us. If you want to send directly to me then I will
look at it: me@...Please note that
I did not compile the code above and there may be
typos, but it should give you a starting
point.

 
Answer #10    Answered By: Gustavo Taylor     Answered On: May 05

It sounds as though you are calling a method from
your main() that is not declared as static.1)
Try adding the "static" keyword to the method,
or2) Move the method call to the
constructor:public class  MyClass { public int x; MyClass()
{ this.x = methodCall();
System.out.println("int value = " + this.x); } public int
methodCall() { //not static! return 2; //return an int
} public static void main(String [] s) {
MyClass mc = new MyClass();
}}HTHepenakPS you wrote "hope all of you get up on the
right
side of the bed this time."1) Sometimes we
beat up on people in a random way just to keep
everybody on their toes!2) The folks here are surly,
unruly and unpredictable!3) We always get up on the
wrong side of bed!:-)

 
Didn't find what you were looking for? Find more on passing variables Or get search suggestion and latest updates.




Tagged: