How to get the variables of a method in a different class in Java?

Hi,
I have two public classes in my java code. Let's say they are:

Class_A and Class_B

And I have a method in Class_A which is called Method_A. This method has a table in it.

Now, I need to use this table in Class_B in Method_B.

How can I do it?

To visualize the problem:

public class Class_A{

    public void Method_A(){
        final Shell shell = new Shell(display);
        Table table = new Table(compTab3, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
        String[] titles = { "Something1", "Something2" };
       SOME  OTHER CODE IN HERE
    }
}

Open in new window



public class Class_B{

    public void Method_B(){
        IN HERE I NEED TO USE table, shell and titles from METHOD A
    }
}

Open in new window

TolgarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

unknown_routineCommented:
You cannot do it as is. Currently you put the table variable is in the scope of Method_A only.

So it will not be visible outside of this method.

You have 2 options:

1: Make table variable  a Class level public variable

2: Modify Method_A to return the Table instead of a void.

Since you want to acces multiple values from Class A , you need to use the 1st method.
0
unknown_routineCommented:
public class Class_A{

public Table table;
public  String[] titles

    public void Method_A(){
        final Shell shell = new Shell(display);
        table = new Table(compTab3, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
        titles = { "Something1", "Something2" };
   
    }
}


now you can access table and titles from the class_B
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.