1

I have a string array (variable) inside method A which is present in class A. Now I want to access it and set it with another string array from a method B which is in class B. class B is in class A.

I am a beginner in Java, so any help is really appreciated. Many thanks.

public class A {
    B myclassb;

    void methodA() {
        String[] myvar;
    }
}

public class B {
    void methodB() {
        // how do I get to A.methodA.myvar?
    }
}
2
  • If the method/array in class B is not static you will have to create an instance of class B then call the method from class B passing the array from class A as a parameter... Commented Jul 30, 2015 at 20:06
  • 1
    improve your description with code please Commented Jul 30, 2015 at 20:11

2 Answers 2

1

It is not completly clear what you want to achieve but i will try to answer anyways. Option 1: You said you want to assign a variable in method a from method b in a nested class. That is not directly possible since function-variables are not accessible from another function and do no longer exist when the function has finished its execution. so you could transfer it as a input-parameter:

public class A {
    public void a(String[] input){
        String[] theArray = input;
    }

    private class B{
        private void b(){
            String[] input = new String[] {"an", "awesome", "Test"};
            a(input);
        }
    }
}

Option 2: Use a member-variable:

public class A {
    private String[] theArray;
    public void a(){
        this.theArray = new String[] {"a", "nice", "Test"};
        B bObject = new B();
        //modify value within b():
        bObject.b();

        //or assign it using a return value:
        this.theArray = bObject.b2();
    }

    private class B{
        private void b(){
            theArray = new String[] {"an", "awesome", "Test"};
        }

        private String[] b2(){
            return new String[] {"an", "awesome", "Test"};
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Are you looking for something to show how Java passes by reference like the following:

public class Test {


    public Test() {

    }

    public void methodA() {
        String [] arr = new String[3];
        arr[0] = "One";
        arr[1] = "Two";
        arr[2] = "Three";
        printArray(arr);

        methodB(arr);

        printArray(arr);
    }

    public void methodB(String [] arr) {
        arr[0] = "A";
        arr[1] = "B";
        arr[2] = "C";
    }

    public void printArray(String [] arr) {
        for (int i = 0; i < 3; i++) {
            System.out.println(arr[i]);
        }
    }

    public static void main(String [] args) {
        Test test = new Test();
        test.methodA();
    }
}

This will output: One Two Three A B C

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.