1

I was reading about arrays in java and I made a code for calculating the number of appearances of all numbers in an array .

public class Example {

    static int b[] = new int[13]; // I can not do static int b[] = new int[a.length] because a[] in not static array
    static int count = 0;

    public static void main(String[] args) {
        int[] a = { 2, 3, 4, 3, 3, 5, 4, 10, 9, 1, 9, 11, 15 };
        counting(a);
        printCount();
    }

    private static void printCount() {
        int k = 0;
        for (int i = 0; i < b.length; i++) {
            System.out.print("number" + " " + a[k] + " " + "is found" + " "); // here I get error in a[k] because it is not static , eclipse says : a cannot be resolved to a variable
            System.out.println(b[i] + " " + "times");
            k++;
        }
        System.out.println();

    }

    private static void counting(int[] a) {
        for (int i = 0; i < a.length; i++) {
            for (int k = 0; k < a.length; k++) {
                if (a[i] == a[k]) {
                    b[i] = ++count;
                }
            }
            count = 0;
        }

    }
}

I got stuck in my printCount() method , there I can not call my a[] array in the method because a[] is not static in the main method . I tried to write static int[] a = { 2, 3, 4, 3, 3, 5, 4, 10, 9, 1, 9, 11, 15 }; in my main method but eclipse does not accept that . How can I make a[] a static array so that can be reached in all methods in my Example class above ?

Thank you

7
  • 2
    Move it out of the method-only scope... It should be declared in the area where b[] is declared. Commented Jun 1, 2016 at 20:36
  • 2
    You could either make it a global variable like you did with b and count. Or you could send the array as a parameter to the method with printCount(a); and change the signature of the method to private static void printCount(int[] a). Commented Jun 1, 2016 at 20:37
  • thank you ,, can not I leave it in the main method ? is there a way to handle that without moving it next to b[] array Commented Jun 1, 2016 at 20:38
  • What I don't understand is how you managed to do this with counting but you are stuck on printCount. Commented Jun 1, 2016 at 20:39
  • @ Gendarme I wanted to make a static array in the main method thank you Commented Jun 1, 2016 at 20:41

3 Answers 3

4
 public static void main(String[] args) {

        int[] a = { 2, 3, 4, 3, 3, 5, 4, 10, 9, 1, 9, 11, 15 };
        counting(a);
        printCount(a);
    }

Pass the array in the printCount() method.

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

4 Comments

thank you ,, can not I do static array in main method?
Sorry. I didn;t get you. It is in main method only. Just pass it in the printCount() method so that you can access it.
thank you so you mean that I can not do like this in main method : static int[] a = { 2, 3, 4, 3, 3, 5, 4, 10, 9, 1, 9, 11, 15 };
@Joe No you cannot. A static variable means that the variable belongs to the class and not to the objects of the class. However, when you are inside a method all the variables are local and are destroyed when you exit out of the method. The variables do not belong to the class. Words like static, public, private, protected do not mean anything inside a method.
2

Why do you want a[] to be static if you don't want it moved out of the main method? The only way it can be accessed outside main() is if it was passed through. No way to call Example.a[] like a normal static variable. Seems to me like you need to get the length of a[] after it is initialized, and then set the bounds for b[] all within the main method.

Comments

2

You can move the a array out to the class scope as static. Then for your array practicing you can easily just change the a array.

However, as others also mention I recommend you to study scopes in Java .

public class Example {

    static int[] a = { 2, 3, 4, 3, 3, 5, 4, 10, 9, 1, 9, 11, 15 };
    static int[] b = new int[a.length];
    static int count = 0;

    public static void main(String[] args) {
        counting();
        printCount();
    }

    private static void printCount() {
        int k = 0;
        for (int i = 0; i < b.length; i++) {
            System.out.print("number" + " " + a[k] + " " + "is found" + " "); 
            System.out.println(b[i] + " " + "times");
            k++;
        }
        System.out.println();

    }

    private static void counting() {
        for (int i = 0; i < a.length; i++) {
            for (int k = 0; k < a.length; k++) {
                if (a[i] == a[k]) {
                    b[i] = ++count;
                }
            }
            count = 0;
        }

    }
}

Comments

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.