Why should a static field be accessible in a static way?

public enum MyUnits { MILLSECONDS(1, "milliseconds"), SECONDS(2, "seconds"),MINUTES(3,"minutes"), HOURS(4, "hours"); private MyUnits(int quantity, String units) { this.quantity = quantity; this.units = units; } private int quantity; private String units; public String toString() { return (quantity + " " + units); } public static void main(String[] args) { for (MyUnits m : MyUnits.values()) { System.out.println(m.MILLSECONDS); System.out.println(m.SECONDS); System.out.println(m.MINUTES); System.out.println(m.HOURS); } } } 

This applies to the post .. cannot reply or comment on any new created. Why mine

 System.out.println(m.MILLSECONDS); 

Providing alerts. Should the static field MyUnits.MILLSECONDS be available in a static way? Thank.

+50
java static
Apr 12 2018-11-11T00:
source share
3 answers

Because when you access a static field, you have to do it in a class (or in this case an enumeration). As in

 MyUnits.MILLISECONDS; 

Not for instance, as in

 m.MILLISECONDS; 

Change To solve the question of why: In Java, when you declare something as static , you say that it is a member of a class, not an object (hence why it is unique). Therefore, it makes no sense to access it on an object, since this particular data element is associated with a class.

+93
Apr 12 2018-11-11T00:
source share

There is actually a good reason:
Non-static access does not always work due to ambiguity .

Suppose we have two classes: A and B, the latter being a subclass of A, with static fields with the same name:

 public class A { public static String VALUE = "Aaa"; } public class B extends A { public static String VALUE = "Bbb"; } 

Direct access to a static variable:

 A.VALUE (="Aaa") B.VALUE (="Bbb") 

Indirect access using an instance (gives a warning to the compiler that VALUE should be statically accessed):

 new B().VALUE (="Bbb") 

So far, so good, the compiler can guess which static variable to use, something that is somehow far away in the superclass seems logical.

Now that is getting complicated: interfaces can also have static variables.

 public interface C { public static String VALUE = "Ccc"; } public interface D { public static String VALUE = "Ddd"; } 

Let me remove the static variable from B and observe the following situations:

  • B implements C, D
  • B extends A implements C
  • B extends A implements C, D
  • B extends A implements C where A implements D
  • B extends A implements C where C extends D
  • ...

The statement new B().VALUE now ambiguous , since the compiler cannot decide which static variable had in mind , and will report this as an error:

error: link to VALUE is ambiguous
the VALUE variable in C and the VALUE variable in D correspond

And for this very reason, static variables must be available in a static way.

+56
Aug 25 '14 at 21:25
source share

Because ... it ( MILLISECONDS ) is a static field (hidden in the enumeration, but what it is) ... however, it is called on an instance of this type (but see below how it is actually not true 1 ).

javac will "accept", but it really should be MyUnits.MILLISECONDS (or not a prefix in the applicable area).

1 Actually, javac โ€œrewritesโ€ the code in the preferred form - if m null , it will not throw NPE at runtime - it is never actually called on the instance).

Happy coding.




I donโ€™t see how the title of the question fits the rest :-) More precise and specialized headers increase the likelihood that the question / answers may benefit other programmers.

+10
Apr 12 2018-11-11T00:
source share



All Articles