-2

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
Dan
  • 3,647
  • 5
  • 20
  • 26
  • 1
    Using proper OOP principles, pass the array instance around via method parameters and return values. Though for trivial (e.g., small, single-file) applications, using static fields might be okay. Also, according to standard Java naming conventions, method names should use `camelCase` (first letter lowercase) and class names should use `CamelCase` (first letter uppercase); you have it backwards. – Slaw May 28 '22 at 10:40
  • Does this answer your question? [What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-or-cannot-resolve-symbol-error-mean) – QBrute May 28 '22 at 10:50
  • I found an other solution, to just add a public static to the variable and defining it in the insertionsort method. Now it works. Thanks for your advice, I corrected my naming ;) – Balazs Miskey May 28 '22 at 10:52

4 Answers4

0

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]);
    }
}
Jhanzaib Humayun
  • 1,193
  • 1
  • 4
  • 10
0
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]);
  }
}
Laisender
  • 714
  • 1
  • 5
  • 10
0

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]);
  }
}
0

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) {
    unsortedListPrintout();
    insertionSort();
}

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

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

}

itfedorovsa
  • 126
  • 1
  • 3
  • 12