-1

I wanted to ask that what is the default access specifiers/modifiers for an array in java? For example if I write

int arr[];

What will it be by default? Is it public abstract final or public by default?

I am asking this question because I am unable to understand comment made by Tom Ball.

I found why default serialVersionUIDs for arrays were different. That calculation includes the class's modifiers, and it turns out all arrays are "public abstract final", not "public" (a new "gotcha" Java interview question :-). Changing that in the runtime, and now all arrays have the same UIDs, even different object classes.

Here is the link https://groups.google.com/d/msg/j2objc-discuss/1zCZYvxBGhY/ZpIRKPLFBgAJ

Can Someone explain?

harshlal028
  • 1,339
  • 1
  • 14
  • 23

2 Answers2

2

The post that you linked refers to the access modifiers of the (dynamically created, synthetic) class object that represents the array: int[].class in your case.

There is no relation between the modifiers of a class and the modifiers of a field.

Think of it like this: the class java.lang.String is public, but you are free to make a private field of type String.

Erwin Bolwidt
  • 28,093
  • 15
  • 46
  • 70
2

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.

Holger
  • 243,335
  • 30
  • 362
  • 661