Question

In the following code, field1 does not have a modifier: public/protected/private

public class class1 {
    String field1;
}

So what is the modifier of field1?

In the Modifier definition in Java, these are all the Modifiers that are relevant to a field:

    Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
    Modifier.STATIC         | Modifier.FINAL        | Modifier.TRANSIENT |
    Modifier.VOLATILE;

Which of the modifier(s) does field1 have? I feel it does not have any of Modifier.PUBLIC,Modifier.PROTECTED, and Modifier.PRIVATE

I ask this because I want to access this field one via Java reflection.


Thanks. For the answers.

It indeed has no modifier.

I have tested getModifiers() for field1. It returns 0.

Thanks.

Was it helpful?

Solution

It has no modifier. For further details what this lack of modifier means see here:

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

OTHER TIPS

Its modifier is known as default. It is every variable/function's modifier that does not explicitly tell about any other modifier.

There is no modifiers. You can access to field from class, package, but not from subclass or another package.

It dose have modifier--default, which is always implict. A field or a method modified with default is accessable to classes that share the same package with the field or method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top