Why does Java allow access to a static member with an instance of an object

I understand that static members belong to a class. Why then does Java allow me to access them using an object?

To understand what I mean, see the following example:

public class Student {
  public static int number = 0;
}

Here numberis a static field that belongs to the class Student, but I can still access it, as shown below:

Student s = new Student();
int n = s.number;

What is the rationale for this decision?

+5
source share
3 answers

The rationale for this is that the object is an instance of the class, and therefore it must have access to each attribute belonging to the class, in addition to instance-level attributes.

. , ( ), , . , ( ), , .

- , ++. , s.number , . Student.number, , number . , , # , , .

+7

, java- hoodoo , , , , . IDE .

, : Java?

, . , . : DONT!

+4
0

All Articles