5

My understanding is that static members belong to the class. Why then does Java allow me to access them with an object?

To understand what I mean, please see the following example:

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

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

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

What is the rationale behind this decision?

missingfaktor
  • 90,905
  • 62
  • 285
  • 365
Myd
  • 103
  • 6
  • I tried to edit your question for clarity but couldn't. I can't understand what are you asking? – Soufiane Hassou Jun 16 '12 at 09:35
  • 1
    Google Translate at its best :) – buc Jun 16 '12 at 09:38
  • 1
    @SoufianeHassou, perhaps my edit can help. – missingfaktor Jun 16 '12 at 09:42
  • are you sure the code compiles in this case? I know for a fact c# throws a compilation error – Baz1nga Jun 16 '12 at 09:43
  • 1
    Whol talked about C# here. Can you please specify from where are you calling `int n = s.number;` – m0skit0 Jun 16 '12 at 09:50
  • @Baz1nga yes it works fine... but with a warning. – UmNyobe Jun 16 '12 at 09:50
  • 1
    @m0skit0, please be sure to check the [edit history](http://stackoverflow.com/posts/11062293/revisions). The original wording of the question was a horrible mess. – missingfaktor Jun 16 '12 at 09:53
  • you just can... http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – Francisco Spaeth Jun 16 '12 at 09:55
  • 1
    possible duplicate of [Why isn't calling a static method by way of an instance an error for the Java compiler?](http://stackoverflow.com/questions/610458/why-isnt-calling-a-static-method-by-way-of-an-instance-an-error-for-the-java-co) – funkybro Jun 16 '12 at 10:05
  • Just because you can do it, doesn't mean you should. You should always access a field in a static way, like using `Student.number;` instead of `s.number;` which wasn't the question, but just pointing that out. – Austin Jun 16 '12 at 10:24

3 Answers3

7

The rationale behind this is that an object is an instance of the class, and therefore it should be able to access every attribute that belongs to the class in addition to the the instance level attributes.

It's like traditional mail. If you receive a mail that were addressed to your whole family (static member), you will feel licenced to open it, because you are the member of the family. On the other hand, if the mail is addressed to you (instance member), only you have the right to open it, no one else in you family.

This also works the same in other object-oriented languages, like C++. However, it is discouraged to use the s.number notation to access static members, as it is misleading for the readers of your code. You should always use the Student.number notation, as this clearly shows that number is a static member. Modern languages, for example C# will issue a warning if you access static members via instance variable, but still it is perfectly legal according to the language specification.

Shubham Goyal
  • 1,254
  • 4
  • 19
  • 24
buc
  • 6,268
  • 1
  • 34
  • 51
  • Thank you for your answer,you have sloved my puzzle dom .Beausce I use C#,so i have this. – Myd Jun 17 '12 at 10:11
4

After some reading apparently The java compiler does the necessary hoodoo to deal with object instances that access static variables and it knows what you want you meant.. IDEs will give you a warning about it ..

Regarding the why and explanation read this: Why isn't calling a static method by way of an instance an error for the Java compiler?

ultimately it is a language spec and you just need to deal with it.. Its your call whether to use it or not.. My suggestion: DONT!

Community
  • 1
  • 1
Baz1nga
  • 15,485
  • 3
  • 35
  • 61
0

The rationale behind this is... JLS - Chapter 8.3.1.1. static Fields

Behe
  • 7,572
  • 3
  • 33
  • 46
  • 4
    I don't see any rationale per-se in the JLS: just the plain fact that you can access it on an object. – Luca Geretti Jun 16 '12 at 09:57
  • +1 for reference. It's a choice they made when designing the language. – dacwe Jun 16 '12 at 10:02
  • 1
    You are right, not a rationale per-se. The original question asked why he was able to do this. So I think the link might be helpful. – Behe Jun 16 '12 at 10:08