0

I have 2 files in one folder. In file "X" i use variables from file "Y" with "Y." successfully. But i now would like to use arrays and variables generated in function "A" inside file "X", in function "B", still inside file "X".

I tried similar formats to "Y." and nothing seems to yield success. I would really like to avoid forcing function "A" to return all these arrays and variables, then pass them all through function "B"'s parameters.

EDIT:

This is my first post and i can't seem to vote or comment on anyone's responses- thank you for the help, i'm sorry my question was sub-par, i tried to be specific. I don't see a way i could clear anything up by typing code, there's quite a lot of it.

My question was essentially answered.

I am told that i cannot use or alter a variable outside of the function it was created in. I wanted to do this with something similar to the method i know works between files. I am currently using the format

<fileY>.<variableinfileY>

to access file Y's variable in this new file X. I thought that if i could do that, then i could use a similar method to access variables in other functions within the same file, but oddly enough it's not possible.

For anyone curious, one form which may solve a similar issue is

Arrays.copyOf(<arrayname>, <arrayname>.lengh)

, but for me this does not help, and i must return variables in one funcion and pass the variables through the new function as parameters.

EDIT EDIT:

You guys really want code :) haha

<afunctionname>{
    char[][] thisarray = new char[cols][rows];
}

<DIFFERENTfunctionname>{
    <this is where i want to use thisarray>
}
CoshCaust
  • 17
  • 4

2 Answers2

1

Assuming this is what you're talking about:

File X

public class FileX(){
    public void functionA(){

    }
}

File Y

public class FileY(){
    private void functionB(){

    }
}

Yoou could always declare the variable/array publicly outside of functionA and access it like normally with fileX.variable, i.e

public class FileX(){
    int[] variableFromA;
    public FileX(){

    }
    public void functionA(){
        this.variableFromA = new int[]{0,1,2,3,4,5};
    }
}

public class FileY(){
    private void functionB(){
        fileX = new FileX();
        fileX.functonA();
        fileX.variableFromA; //This should give you the variable/array you want;
    }
}

In response to your edit with the code, I'll say it's possible to do this with an array,if the array was declared in the calling function first. I'll illustrate with code.

File X

public class FileX{
    public void aFunctionName(char[][] thisArray){
        thisArray[0][0] = 'X';
    }
}

File Y

public class FileY{
    public void callingFunction(){
        FileX x = new FileX();
        char[][] before = new char[2][2];
        before[0][0] = 'Y';
        x.aFunctionName(before);
        System.out.println(before[0][0]); //This prints 'X' and not 'Y'
    } 

    public static void main(String[] args){
        FileY y = new FileY();
        y.callingFunction();
    }
}

The reason this works is because in Java an array in an object, and objects are not passed directly to the method, instead the position of the object is passed to the method. See this Stackoverflow answer for more details

Community
  • 1
  • 1
Olumide
  • 758
  • 13
  • 31
1

I'm not entirely sure what you mean, especially regarding file X and file Y. But "sharing" a variable (e.g. an array) across functions (methods) is as easy as this:

 public class X { 
     private int[] myArray; 

     private void a() { 
         myArray = new int[1]; 
         myArray[0] = 1; 
     } 

     private void b() { 
         System.out.println(myArray[0]); 
     } 
}
yaccob
  • 1,230
  • 13
  • 16