-1

I seen this bit of code, Even though I think I got the concept of static in Java I am bit confused. Can any one explain it to me, how an object being static works?

My code:

package com.oracle.certification.sampleTest;

public class Person {
    static class Mail {
        static String desc="Male";
    }

    static Gender Mail=new Gender();
}

package com.oracle.certification.sampleTest;

public class Gender {
    String desc="Gender";
}

package com.oracle.certification.sampleTest;

public class Human {
    public static void main(String str[]) {
        System.out.println(Person.Mail.desc);
    }
}

When the class Human is run, the O/P is 'gender' not 'male', even though des= gender is nonstatic and des=male is static with static inner class. Also I don't need to import the classes in Hman? I am sorry that I have very little knowledge about inner classes, first of all.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Gpar
  • 181
  • 1
  • 1
  • 7
  • 1
    An object is not `static`. That is not a concept that exists. A variable can be declared `static`. – Sotirios Delimanolis Nov 03 '14 at 06:42
  • 1
    Your code is further confused by the use of `Mail` as both a class name and a field name... – Jon Skeet Nov 03 '14 at 06:46
  • Java Tutorial [Nested Classes](http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) – DavidPostill Nov 03 '14 at 06:50
  • 2
    Indeed the question shows that that OP doesn't understand scope rules in Java well, but he posted code that highlights his problem: when he prints `Person.Mail.desc`, it doesn't show `Male` like he expects, but instead it shows `Gender`. That's a far better description of a problem than many that I've seen on SO. Cut the OP some slack please. – Erwin Bolwidt Nov 03 '14 at 07:01

1 Answers1

1

Can any one explain it to me, How an object being static works?

Essentially, static in that context means that the entity in question is attached to the class itself, not to an object of the class. Hence, with static there is exactly one instance of what you declare:

class T {
   public static int staticMember = 0;  // one variable stored in memory
   public int nonStaticMember = 0;      // as many variables stored in memory as objects are created from the class
}

See also What does the 'static' keyword do in a class?

However, your question is not necessarily a misunderstanding of static classes, but a corner case of name resolution: You are declaring both a type and a member variable with the same name (Mail) in one scope (within the Person class) - while one might think that this should not even be possible, the Java language does allow this and defines a couple of rules to determine which one to use.

From the JLS:

A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type, or a package. In these situations, the rules of §6.5 specify that a variable will be chosen in preference to a type.

...
static class Mail {   // a Type
}

static Gender Mail ...   // a Variable with the same name 
                         // - the compiler chooses to use this one
...

Hence, when referencing Person.Mail, the compiler chooses the variable, not the type (the inner class).

Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • Thanks, This was the another concept that I dint even think of. Thanks for letting me know. Now my question is how the object being static works? I have 4yrs of java development experience but never seen and object as static. – Gpar Nov 03 '14 at 07:11
  • I got it now. After a long struggle. Thanks all, especially @Andreas... – Gpar Nov 03 '14 at 12:10