0

I have some code that is similar to the following:

public class exampleClass {

    public void main(){
        defineVariable();
        nextMethod();
    }

    void nextMethod(){
        list[8] = "threw away";
    }

    void defineVariable(){
        String[] list = {"trivial string","more trivial strings",
                       "just another trivial string","tr","a","sd",
                       "godzilla","ate","my","pony","and homework","trivial"};
    }

}

And I cant access list in nextMethod.How can I fix this problem , it may seem trivial to make such a small array global but the actual arrays are in the hundreds (hence the fact I didnt c+p my actual code although if this is necessary I wont mind in the least).

Thanks very much and as a side note this is in Android although I doubt that that will effect the java code (am I wrong to assume this?).

Anyway thanks again! Ive been using StackOverflow alot lately and have only contributed a bit so from now on I will attempt to answer as many questions as I can.

Thanks,

2
  • 1
    How would you do this if it were something other than an array? Commented Feb 7, 2014 at 8:30
  • This also applies to that although I dont mind passing arguments or merely making it global @OliCharlesworth Commented Feb 7, 2014 at 8:34

3 Answers 3

1

Simply declare it in class.

 public class exampleClass {

    public void main()
    {

    nextMethod(defineVariable());
    }

    void nextMethod(String[] list)
    {
    list[8] = "threw away";
    }

    private String tab[] defineVariable()
    {
    String[] list= {"trivial string","more trivial strings","just another trivial string","tr","a","sd",
                     "godzilla","ate","my","pony","and homework","trivial"};
       return list
    }

    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks rafik although this originally did not work I add new String and it functioned. Thanks again!
1

If you let defineVariable be void and create a variable within its own scope, that variable just disappears into oblivion upon finished exection. Instead, have defineVariable return the list and then use this list as a parameter to the nextMethod() function.

Comments

1

A neat way to store many variables is to group them into different objects. That way variables that are related can be put into the same object and the number of variables in your main class is reduced to a managable number.

In the example below, I have made the variables public. If you prefer, you can create getters and setters instead.

public class Actions {
  public String humans[] = { "sit", "stand", "eat", "walk", "run", "drive", ride" };
  public String dogs[] = { "sit", "stand", "bark", "run" };
  public String sharks[] = { "swim", "attack" };
}

public class Equipment {
  public String armour[] = { "leather", "chainmail", "plate" };
  public String weapon[] = { "sword", "axe", "dagger" };
}

2 Comments

If its not asking to much can you provide an example? Thanks.
Thanks very much indeed! This has helped alot. Although I will use this I think Rafik's answer was more befitting. Although thanks again and it pains me that I am unable to select more than one answer.

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.