Below, I have written simple code that re-creates problems that have emerged as my application is growing.
Problem Statement:
I have a class that needs to be used to create polymorphic objects which each require a variable number of arrays as input. So I might have two to five of these objects instantiated throughout the time my app is running. And between them, these objects might have 15 arrays. What’s more, some of these arrays need to be modifyable from a variety of different, other objects after they are created, not just by the objects that created or “house” them.
Three Questions:
1.) What is the most stable/recommended way of passing a number of different arrays into a class? I have used a varargs approach below, but am not sure if this is the best way.
2.) How can I avoid problems due to the static key word? You will see that the code in Main.java below throws errors related to the static key word. I hesitate to allow all instances of the class to share a single copy of the array. All of this code is in an internal frame in the GUI, and I may later want to allow the user to create multiple internal frames, each with their own instances of all this code for completely different data sets.
3.) How can I enable various other objects to edit and otherwise manipulate these arrays after the arrays have been created? You can see below that I am passing arrays to Another.java, which then assigns those arrays to local class variables before editing the array. Is the local array the same object as was passed into Another.java, or have I made an error by creating duplicate arrays?
Code Samples:
The code samples below to illustrate what I need to do.
Can anyone show me a re-written version of the code below that fixes these problems?
Also, I would very much appreciate any links you can send to articles about how these problems are best addressed.
Main.java
public class Main {
int arrayLength = 10;
double[] array1 = new double[arrayLength];
double[] array2 = new double[arrayLength];
double[] array3 = new double[arrayLength];
double[] array4 = new double[arrayLength];
double[] array5 = new double[arrayLength];
static void makeHandleArrays(){// how do I avoid static errors thrown here?
HandleArrays firstHandleArrays = new HandleArrays();
firstHandleArrays.arrayLength(array1);
HandleArrays secondHandleArrays = new HandleArrays();
secondHandleArrays.arrayLength(array1,array2,array3); //varargs approach
}
public static void main(String[] args){//how do I avoid static errors thrown here?
makeHandleArrays();
Another myOtherClass = new Another(array1,array2);
}}
HandleArrays.java
public class HandleArrays {//Is this varargs approach the best way to handle a variable number of arrays as inputs?
double[][] dataArrays;
int numArrays;
void arrayLength(double[] ...ds){
dataArrays = new double[ds.length][];
numArrays = ds.length;
for(int i = 0;i<ds.length;i++){
dataArrays[i]=ds[i];
}
}
}
EditArrays.java
public class EditArrays {
double[] thisHereArray=new double[10];
EditArrays(double[] arrayToEdit){
thisHereArray=arrayToEdit;
thisHereArray[6]=4.678;//does this edit the same object that was passed into the class? Or did I make an error with the name change?
}
}
Another.java
public class Another {
double[] localArray1;
double[] localArray2;
Another(double[] anArray){
localArray1=anArray;
EditArrays myEditArrays = new EditArrays(localArray1);// does the name change mean that this line is NOT editing the same anArray?
}
Another(double[] anArray, double[] anotherArray){
localArray1=anArray;
localArray2=anotherArray;
EditArrays myEditArrays = new EditArrays(localArray1);// does the name change mean that this line is NOT editing the same anArray?
EditArrays anotherEditArrays = new EditArrays(localArray2);// does the name change mean that this line is NOT editing the same anotherArray?
}
}
There seem to be a number of different concerns happening here, so I’ll try to address them.
You’re not actually using
varags– you’re usingoverloading. Rather than having multiple methods with increasing numbers of variables, you can simple declare:Which will give you an array of the input data type (so, you’ll end up with an array of arrays, in this case). Oftentimes, the varags will be referenced in a
foreachloop. The only condition on their use is that they have to be declared as the last parameter; it will also be important to remember that actually providing them is completely optional.statickeyword issuesThe issue you’re facing with the
statickeyword is that you’re attempting to access instance variables inside a static (class-level) method.staticmethods can only accessstaticvariables and variables they were provided as part of the input parameters. In order to get around the ‘static errors’, you’re going to have to pass either the instance of the class in (via athisreference), or a specific array, or modify the array reference to be static:Static references:
Passing in instance reference:
You seem to have some confusion over how java handles input parameters.
Java handles all input parameters as pass-by-value, sometimes known as pass-a-copy. This is covered in detail elsewhere on this site, so I’m not going to go through all of it here.
What it means for your code, though, is this:
actually modifies one array – to which there are three (or more) references. That is,
thisHereArray, andarrayToEdit, as well as whatever reference (say,localArray1fromAnother) was passed in, all point to the exact same array in memory. This code produces an equivalent effect:All you’ve done is duplicated the references, not the actual array. Depending on your requirements, this could be good or bad. In a multithreaded environment, this will cause a huge number of (nearly untraceable/reproducable) errors. In a single-threaded environment, you’re probably fine.
Without knowing your exact requirements, it’s a little hard to give additional feedback. I can give some general recommendations, though:
Collectionsframework.variableSameTypeSameName1,variableSameTypeSameName2. You either have a collection of the same things (like you appear to have here) and they should all be put into a collection (array), or they all represent different things and should be named as such (peopleToInvite,peopleToExclude,peopleToKill).System.out.println()inside of ‘regular’ or production methods (unless you’re making a console application, which I doubt). It’s fine for debugging/miscellaneuos testing when you’re checking things.an internal frame in the GUI, although you don’t provide any Swing/AWT code. Never mix any UI code with ‘backend’ or model code directly (it’s hard to tell what you’re attempting, so this may not be an issue) – the headaches it causes should be avoided.