2

I can't seem to find an answer for this, I have a lot of different arrays, and I want to stuff all of them into one class, to make my main one neater.

Testy obj = new Testy();

But i have no clue on how to get an array from here. Sorry if this is a bad question, this is my first time posting on the site. I'm just trying to get an array called testArray from the class called Testy.

Code inside Testy:

public class Testy {
public static void ArrayTesting(){
    int testArray[]={56,1,4,175};
}}
4
  • 2
    You need to show us where the arrays you want are now, and where you want them to be. Mar 23, 2016 at 0:44
  • 1
    Yes it's a bad question because you're not telling any of the necessary details. Please ask it with en eye towards our situation: folks who have no idea of what your code looks like or what you've tried and how it's not working. Mar 23, 2016 at 0:44
  • 1
    Don't only describe your code, post it. To do so use edit option.
    – Pshemo
    Mar 23, 2016 at 0:44
  • post the codes in your Testy() class
    – john
    Mar 23, 2016 at 0:46

1 Answer 1

5

The standard way to get any variables from another class is to create a getter for that particular variable. After that, you can call the getter in order to get the value of whatever variable you want.

Example:

class MyClass
{
    //Variable I want to return
    private int[] numArray = {1, 2, 3};

    public int[] getNumArray() {
        return numArray.clone();
    }
 }

and you can call it by saying

MyClass newClassObj = new MyClass();
int[] secondArray = newClassObj.getNumArray();
1
  • 3
    Arrays are mutable. It's better to clone it before return to the caller
    – dragon66
    Mar 23, 2016 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.