Home > Back-end >  'Array()' has private access in 'java.lang.reflect.Array' when trying to declare
'Array()' has private access in 'java.lang.reflect.Array' when trying to declare

Time:07-18

I can't figure out what's wrong I am doing here .

import java.lang.reflect.Array;
import java.util.Arrays;

public class arrays_3 {
    public static void main(String[] args) {
        Array arr = new Array(5);
    }
}

I have already imported the required classes here. but when i am trying to create new array then it gives me an error that : 'Array()' has private access in 'java.lang.reflect.Array' along with that in my instructor's IDE its working properly without importing anything. please take a look at my instructor's screen

CodePudding user response:

Arrays should be declared like this

int intArray[]; 
or int[] intArray;

For you it can be something like this:-

public static void main(String[] args) {
    int[] intArray = new int[20];
}

CodePudding user response:

The better way to do this is :

int intArray[]; 
or int[] intArray;

and in your code, you use public access modifier and it needs a class named Arays.java. The problem is that why you are stuck here because your instructor has made another class in the same package named Arrays but according to java docs, it is very bad to use the same name as a standard class for your own classes!

and then initialize your array like this

public static void main(String[] args) {
    int[] intArray = new int[5];
}
  • Related