When you write a declaration like
int arr[];
you are declaring a variable. This variable has exactly those access modifiers, you are declaring. If you don’t specify any modifiers on a class variable, it will be package-private by default. Note that variables can never be abstract
.
The reason why you don’t understand the cited text is, that you are confusing the class modifiers of the variable’s type with the variable’s modifiers.
Letting arrays aside, if you declare a variable like Foo bar
, then the class Foo
has modifiers independently of the modifiers of the variable bar
.
Now, the same applies for array types. In Java, arrays are objects, hence have a class type. At runtime, you may invoke getClass()
on an array just like on any other object and you will get a Class
object representing a synthetic class. You can also access an array class via class literal:
Class<?> clazz=int[].class; // int[]
int mod=clazz.getModifiers();
if(Modifier.isPublic(mod)) System.out.print("public ");
if(Modifier.isAbstract(mod)) System.out.print("abstract ");
if(Modifier.isFinal(mod)) System.out.print("final ");
System.out.print(clazz);
System.out.print(" implements");
for(Class<?> cl:clazz.getInterfaces())
System.out.print(" "+cl.getName());
System.out.println();
It will print
public abstract final class [I implements java.lang.Cloneable java.io.Serializable
showing that the class representing the array type int[]
has the special name [I
and the modifiers abstract
and final
, a combination that is impossible for ordinary classes.
Note that an array class is public
, if either it’s an array of primitive values or its element type is public
as well. As explained, this doesn’t stop you from declaring non-public
variables of such a type.