Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i access an object from another method in java?

Tags:

java

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
    }
}
like image 789
Lloyd aaron Avatar asked Apr 16 '15 15:04

Lloyd aaron


2 Answers

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();
    }
}
like image 160
Aziel Ferguson Avatar answered Sep 23 '22 06:09

Aziel Ferguson


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
like image 24
Lawrence Aiello Avatar answered Sep 20 '22 06:09

Lawrence Aiello