Method that returns multiple variables

Is it possible to pass several variables to a method, change those variables and return all of the variables to the caller making the changed values visible to the caller?
[178 byte] By [Ylzzirga] at [2007-10-2 23:36:09]
# 1

Basically no, although there are ways around it.

What are these variables? A better solution might be to create a class that encapsulates that set of variables, and then write a method that takes an object of that class and returns a different object of that class (with the variables so changed).

paulcwa at 2007-7-14 16:18:28 > top of Java-index,Java Essentials,New To Java...
# 2

Yes it can be done:public class TotallyUnrelatedBunchOfValues {

public String justAValue;

public int anAint;

public ResultSet rs;

public JFrame frame;

// etc. etc.

}

...

public TotallyUnrelatedBunchOfValues foo(TotallyUnrelatedBunchOfValues tubov) {

...

// alter anything in that tubov

...

return tubov;

}

I hope you do understand the design flaw of this all.

kind regards,

Jos

JosAHa at 2007-7-14 16:18:28 > top of Java-index,Java Essentials,New To Java...
# 3

Yeah...you can easily do that,use a collection or an array to store the changed values and return that back....something like this:

public ArrayList func(String val1,String val2){

ArrayList<String> data = new ArrayList<String>();

//put your logic

data.add(val1);

data.add(val2);

return data;

}

Hope that helps....

I thought that was the way..but if "paulc" says its not possible..then he might be correct..because he is an expert.....!!!

java80a at 2007-7-14 16:18:28 > top of Java-index,Java Essentials,New To Java...
# 4
They are class classes objects. Really what I was trying to do is instantiate the classes in one method and allow for them to be visible by another.
Ylzzirga at 2007-7-14 16:18:28 > top of Java-index,Java Essentials,New To Java...
# 5

You can easily instatiate a class in one method and then return the objects so the caller (or whoever) can see them and use them.

But if you're changing anything internal in those objects, it might be better to take that functionality (the stuff in the method that changes the values in question) and move it into the class itself that holds the values.

Get it?If method A instatiates class B, then passes or returns that B object so that method C changes the B object, a better solution might be to take that method C and change it around so that it's part of class B itself.

There are a few cases where folding functionality in like that doesn't make sense (a few, rare cases); in those cases you can make a data object whose fields are final. (That is, the fields are immutable; they can't be changed.) Then a method might take such and object, and return a completely different object with different values.

paulcwa at 2007-7-14 16:18:28 > top of Java-index,Java Essentials,New To Java...
# 6

What I'm trying to do is create a pdf file. After OpenFile returns to main all of the variables (classes) I passed are still null. Would I be better off making those public and not worry about passing them to the method?

Ultimately I plan on adding a couple other methods. One to add content and one to close the file.

public static void main(String[] args) {

PdfReader sReader = null;

PdfWriter sWriter = null;

Document sDocument = null;

PdfContentByte scb = null;

int success = OpenFile(sWriter, sReader, sDocument,scb);

// Passed parameters are still null at this point

}

public static int OpenFile(PdfWriter inWriter, PdfReader inReader, Document inDocument, PdfContentByte incb) {

int OpenSuccess;

try{

//create a reader for a certain document

PdfReader reader = new PdfReader("invoicetemplate.pdf");

//retrieve the size of the first page

Rectangle psize = reader.getPageSize(1);

float width = psize.height();

float height = psize.width();

//creation of a document-object

Document document = new Document(new Rectangle(width, height));

document.setPageSize(psize);

//create a writer that listens to the document

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("NewInvoice.pdf"));

//we open the document

document.open();

//add content

PdfContentByte cb = writer.getDirectContent();

document.newPage();

PdfImportedPage page1 = writer.getImportedPage(reader, 1);

cb.addTemplate(page1,0,0);

// Set passed parameters equal to objects created

inWriter = writer;

inReader = reader;

inDocument = document;

incb = cb;

OpenSuccess = 0;

}

catch(Exception e) {

System.out.println(e.getMessage());

OpenSuccess = -1;

}

return OpenSuccess;

}

Ylzzirga at 2007-7-14 16:18:28 > top of Java-index,Java Essentials,New To Java...
# 7

*sigh*

public class ArgumentStuff

{

public PdfReader sReader;

public PdfWriter sWriter;

public Document sDocument;

public PdfContentByte scb;

}

...

ArgumentStuff arguments = new ArgumentStuff();

int success = OpenFile(arguments);

// arguments.sReader, etc are not null now

...

public static int OpenFile(ArgumentStuff arguments)

{

...

arguments.sReader = new PdfReader("invoicetemplate.pdf");

// etcetera to set the other argument elements

...

}

warnerjaa at 2007-7-14 16:18:28 > top of Java-index,Java Essentials,New To Java...
# 8

Or perhaps:

public static void main(String[] args) {

FileInfo fi = getFileInfo(); // queries user, presumably

int status = openFile(fi);

if (status == GOOD) {

processFile(fi);

}

}

or something like that, where FileInfo encapsulates readers and writers, or whatever.

paulcwa at 2007-7-14 16:18:28 > top of Java-index,Java Essentials,New To Java...
# 9
*sigh* Thanks wienerja for the reply. I appreciate the help.
Ylzzirga at 2007-7-14 16:18:28 > top of Java-index,Java Essentials,New To Java...
# 10

paulcw,

I think, form what little I understand, that your suggestion to encapsulate is probably a better way to approach this. Basically setup the reader, writer, document, and contentbyte to be properties of a class and then create methods inside that class to set and get the properties. Can you tell me if I'm way out of bounds on this.

Ylzzirga at 2007-7-14 16:18:28 > top of Java-index,Java Essentials,New To Java...
# 11
If they're a bunch of values that should go together, then it makes sense to write a class that expresses that.Try designing the class, and its package, and its relationships to other classes that use the class, so you can keep the scope of the properties as limited as possible.
paulcwa at 2007-7-14 16:18:28 > top of Java-index,Java Essentials,New To Java...