File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

passing data between 2 classes

Jim Linebarger

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.


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

Joined: Mar 22, 2005
Posts: 42965
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).
Marilyn de Queiroz

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

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

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

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){
private static void methodInA(){
String begin = "This is";
B inst = new B();
String sent1 = inst.firstMethodInB(begin);
String sent2 = inst.secondMethodInB(begin);

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){
private static void methodInC(){
String bng = "This is ";
D inst = new D(bng);
String sent = inst.getFirstSent();

public class D{
private String sent1;
private String begin;
public D(String begin){
this.begin = begin;
private void firstMethodInD(){
sent1 = begin + " the first method in D";
public String getFirstSent(){
return sent1;
I agree. Here's the link:
subject: passing data between 2 classes
It's not a secret anymore!