wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes passing data between 2 classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "passing data between 2 classes" Watch "passing data between 2 classes" New topic
Author

passing data between 2 classes

Jim Linebarger
Greenhorn

Joined: Dec 23, 2005
Posts: 5
I am trying to figure out if it is considered good code if I have a constructor call a method when it is initialized. Have that method call setters. And then use getters to get the data back to the original class.

Or

Should I create an instance in one class for a second class and use that to make calls to different methods in the second class.

Basically I am trying to figure out the proper way to pass data back and forth between classes.

Hope this makes sence.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
Welcome to JavaRanch.

I wouldn't create objects in the constuctor. Instead I'd do that in an "init" method, which gets called right after the constructor, thus separating object creation and initialization. It's a judgement call, though.

Not knowing your code, I'm not sure what alternatives you see to methods, when it comes to passing data between objects (unless you were thinking of making fields public, which is not considered good design).


Ping & DNS - my free Android networking tools app
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
I prefer the second way unless there is some reason that won't work.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Jim Linebarger
Greenhorn

Joined: Dec 23, 2005
Posts: 5
If I do it the second way, don't my methods in the second class have to be public? I would not be able to make them private methods, correct?
Jim Linebarger
Greenhorn

Joined: Dec 23, 2005
Posts: 5
I am not working on anything in paticular just want to know the cleanest way of doing this. I have created a couple simple classes that describe my question. With the classes so simple the question at hand almost seems mute but please imagine they are more involved. Maybe I am all wet and there is a more correct way of doing this. In class D, maybe I should have used a setter.
Maybe somebody could show me an example of how they would do it.

Thanks, Jim

Class A and B go together and Class C and D go together.

public class A{
public static void main(String [] args){
methodInA();
}
private static void methodInA(){
String begin = "This is";
B inst = new B();
String sent1 = inst.firstMethodInB(begin);
String sent2 = inst.secondMethodInB(begin);
System.out.println(sent1);
System.out.println(sent2);
}
}

public class B{
public B(){}
protected String firstMethodInB(String bgn){
return bgn + " the first Method in B";
}
public String secondMethodInB(String bgn){
return bgn + " the second Method in B";
}
}
-----------------------------------------------------------------------
public class C{
public static void main(String [] args){
methodInC();
}
private static void methodInC(){
String bng = "This is ";
D inst = new D(bng);
String sent = inst.getFirstSent();
System.out.println(sent);
}
}

public class D{
private String sent1;
private String begin;
public D(String begin){
this.begin = begin;
firstMethodInD();
}
private void firstMethodInD(){
sent1 = begin + " the first method in D";
}
public String getFirstSent(){
return sent1;
}
}
 
GeeCON Prague 2014
 
subject: passing data between 2 classes