dd_j
dd_j

Reputation: 1

'Array()' has private access in 'java.lang.reflect.Array' when trying to declare array in a class

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

Upvotes: -1

Views: 460

Answers (2)

Monu Rohilla
Monu Rohilla

Reputation: 665

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

Upvotes: 0

RitomG
RitomG

Reputation: 31

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

Upvotes: 3

Related Questions