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).
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
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.....!!!
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.
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;
}
*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
...
}
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.
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.