Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6831993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:48:16+00:00 2026-05-26T22:48:16+00:00

Below, I have written simple code that re-creates problems that have emerged as my

  • 0

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?
}
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:48:16+00:00Added an answer on May 26, 2026 at 10:48 pm

    There seem to be a number of different concerns happening here, so I’ll try to address them.

    • 1) ‘Varags’ approach

    You’re not actually using varags – you’re using overloading. Rather than having multiple methods with increasing numbers of variables, you can simple declare:

    void methodName(dataType...variableName) {
        // method code here
    }
    

    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 foreach loop. 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.

    • 2) static keyword issues

    The issue you’re facing with the static keyword is that you’re attempting to access instance variables inside a static (class-level) method. static methods can only access static variables 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 a this reference), or a specific array, or modify the array reference to be static:

    Static references:

    public class Main {
        static double[] arr = new double[10];
    
        public static void main(String...args) {
            arr[0] = 5;
        }
    }
    

    Passing in instance reference:

    public class Main {
        double[] arr = new double[10];
    
        static void modifyArray(double[] modArr) {
            modArr[0] = 3;
        }
    
        public static void main(String...args) {
            Main inst = new Main();
            modifyArray(inst.arr);
        }
    }
    
    • 3) Editing the arrays externally

    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:

    EditArrays(double[] arrayToEdit) {
        thisHereArray = arrayToEdit;
        thisHereArray[6] = 4.678;
    }
    

    actually modifies one array – to which there are three (or more) references. That is, thisHereArray, and arrayToEdit, as well as whatever reference (say, localArray1 from Another) was passed in, all point to the exact same array in memory. This code produces an equivalent effect:

    EditArrays(double[] arrayToEdit) {
        thisHereArray = arrayToEdit;
        arrayToEdit[6] = 4.678;
    }
    

    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.

    • Providing more feedback

    Without knowing your exact requirements, it’s a little hard to give additional feedback. I can give some general recommendations, though:

    1. Only use arrays for definite or permentantly size-bound collections. Otherwise, use members of the Collections framework.
    2. Don’t create and name variables in the format 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).
    3. Do NOT perform edits of external (passed as parameter) objects inside of a constructor. You can pass a reference to the object to be edited, and call a method afterwards to edit it.
    4. Don’t use 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.
    5. You mention this code is running in 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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a very simple WCF service, that worked fine (code below), then
Below is the code that I have written. I want to do the simple
below i have a code that runs in most of my simple programs ..
I have written a simple application that displays a list of candidates for a
I have a simple standalone application written in Visual Basic that I'm porting to
I have a problem with a very simple piece of code written in Javascript,
Below I have written a sample program that I have written to learn about
I have written code within Python that doesn't release memory the way it should.
I have replicated a stripped-down version of my code that has recently been re-written
I have written a simple single threaded client server application in Visual C++ .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.