7

I have the object numberlist that i created in create() method and i want to access it so i can use it in the question() method.

Is there another way to do this that I probably missed? Am I messing something up? If not, how should I do this to get the same functionality as below?

private static void create() {
    Scanner input = new Scanner(System.in);

    int length,offset;

    System.out.print("Input the size of the numbers : ");
     length = input.nextInt();

     System.out.print("Input the Offset : ");
     offset = input.nextInt();

    NumberList numberlist= new NumberList(length, offset);




}


private static void question(){
    Scanner input = new Scanner(System.in);

    System.out.print("Please enter a command or type ?: ");
    String c = input.nextLine();

    if (c.equals("a")){ 
        create();       
    }else if(c.equals("b")){
         numberlist.flip();   \\ error
    }else if(c.equals("c")){
        numberlist.shuffle(); \\ error
    }else if(c.equals("d")){
        numberlist.printInfo(); \\ error
    }
}
1
  • Declare it as a field, not as a local variable in your method. Commented Apr 16, 2015 at 15:08

3 Answers 3

3

While interesting, both of the answers listed ignored that fact that the questioner is using static methods. Thus, any class or member variable will not be accessible to the method unless they are also declared static, or referenced statically. This example:

public class MyClass {
    public static String xThing;
    private static void makeThing() {
        String thing = "thing";
        xThing = thing;
        System.out.println(thing);
    }
    private static void makeOtherThing() {
        String otherThing = "otherThing";
        System.out.println(otherThing);
        System.out.println(xThing);
    }
    public static void main(String args[]) {
        makeThing();
        makeOtherThing();
    }
}

Will work, however, it would be better if it was more like this...

public class MyClass {
    private String xThing;
    public void makeThing() {
        String thing = "thing";
        xThing = thing;
        System.out.println(thing);
    }
    public void makeOtherThing() {
        String otherThing = "otherThing";
        System.out.println(otherThing);
        System.out.println(xThing);
    }
    public static void main(String args[]) {
       MyClass myObject = new MyClass();
       myObject.makeThing();
       myObject.makeOtherThing();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

You would have to make it a class variable. Instead of defining and initializing it in the create() function, define it in the class and initialize it in the create() function.

public class SomeClass {
    NumberList numberlist; // Definition
    ....

Then in your create() function just say:

numberlist= new NumberList(length, offset);  // Initialization

Comments

3

Declare numberList outside your methods like this:

NumberList numberList;

Then inside create() use this to initialise it:

numberList = new NumberList(length, offset);

This means you can access it from any methods in this class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.