Home > Enterprise >  Variable accessible in every method?
Variable accessible in every method?

Time:05-28

I want to print out my array in my Insertionsort method, but it cannot find it. How do I make my array/variable accessible in all my methods? I am new to Java. Thanks

import java.util.Arrays;


public class insertionsort {

  public static void main(String[] args) {
    UnsortedListPrintout();
    Insertionsort();
  }

  public static void UnsortedListPrintout() {

    int[] ul = new int[] {2, 5, 7, 3, 6, 10, 8, 7, 7, 1};
    System.out.println("Unsorted List: "   Arrays.toString(ul));
  }


  public static void Insertionsort() {
    System.out.println(ul[1]);
  }

}

Error:

error: cannot find symbol

System.out.println(ul[1]);
                   ^
  symbol:   variable ul
  location: class insertionsort
1 error

CodePudding user response:

Make it a class variable:

import java.util.Arrays;


public class insertionsort {
    private int[] ul;

    public static void main(String[] args) {
        UnsortedListPrintout();
        Insertionsort();
    }

    public static void UnsortedListPrintout() {

        ul = new int[] {2, 5, 7, 3, 6, 10, 8, 7, 7, 1};
        System.out.println("Unsorted List: "   Arrays.toString(ul));
    }

    public static void Insertionsort() {
        System.out.println(ul[1]);
    }
}

CodePudding user response:

public class insertionsort {
  
  static int[] ul = new int[] {2, 5, 7, 3, 6, 10, 8, 7, 7, 1};

  public static void main(String[] args) {
    UnsortedListPrintout();
    Insertionsort();
  }

  public static void UnsortedListPrintout() {
    System.out.println("Unsorted List: "   Arrays.toString(ul));
  }

  public static void Insertionsort() {
    System.out.println(ul[1]);
  }
}

CodePudding user response:

To define Global Variable you can make use of static Keyword

public class insertionsort {
    public static int[] ul = new int[] {2, 5, 7, 3, 6, 10, 8, 7, 7, 1};
    public static void main(String[] args) {
    UnsortedListPrintout();
    Insertionsort();
  }

  public static void UnsortedListPrintout() {
    System.out.println("Unsorted List: "   Arrays.toString(ul));
  }


  public static void Insertionsort() {
    System.out.println(ul[1]);
  }
}

CodePudding user response:

Deal with static/non static variables and classes, it is important to continue learning java. Also deal with names of classes and methods (CamelCase): --> (C)lass(N)ame beginning with (Big) letter. --> (m)ethod(N)ame; - beginning with (little) letter. If you need a global variable it should be an instance of the class (defined like a class field (not in the method); -Static variables may use everywhere. A single copy to be shared by all instances of the class. A static variable can be accessed directly by the class name and doesn’t need any object. -Non static variables should not be preceded by any static keyword. These variables can access with object reference.

public class InsertionSort { public static int[] ul = new int[] {2, 5, 7, 3, 6, 10, 8, 7, 7, 1};

public static void main(String[] args) {
    insortedListPrintout();
    insertionSort();
}

public static void insortedListPrintout() {
    System.out.println("Unsorted List: "   Arrays.toString(ul));
}

public static void insertionSort() {
    System.out.println(ul[1]);
}

}

  • Related