Hello! I am trying to relearn java as my experience in java on my work doesn't use an OO point of view. The developer came from a C university, so did the person I replace and well, me too! So, I think it's time to move on.
Ok, here's my question. What does static do in my code? Why did it do that?
Anyway, my friend who has been studying for the SCJP for 2 months (which actually is fun way to study as we both brag about what we know to each other) and says that static is hard to understand.
P.S. The class was inspired by 2 questions in the mock exams, one from the java ranch round up and from another mock exam.
Thanks for your input but I still don't get static. From what I read, static variables are like a class that doesn't care what happens to it surrounding class... or something... nope... I am really lost with this. Can you make an anology of static? I mean like why the output was like that on my code.
Why myX.getString2()); displayed the superclass' (or is it the original variable type X) output? It's like X static methods are always used or not overridded even if the new instance is from Y.
X myX = new Y();
in case of overriding object type define which method to call....here object is of type Y() so class Y method will be called
NON Static: Y spots the mark
but static method cannot be overridden....so class X method will hide the method in class Y...
or you can also say that in case of static method refernce type define which method to call.
so output will be
Static: X spots the mark
Static members belong to the class, not the object. One of the results of this is that polymorphism does not apply to static methods - static methods are never overridden. Instead, the method that gets called depends entirely on the reference type.
So this is what happens...
myX.myMethod() - not static, so polymorphism applies. myMethod() in Y is called because myX is actually an object of type Y.
myX.getString() - not static, polymorphism applies. getString() in Y is called - same situation.
myX.getString2() - static, no polymorphism. myX has the reference type X, so X.getString2() is called.
In practice, you would never write myX.getString2(). You would write X.getString2(). The effect is the same, but the latter makes it far clearer what's going on.
Joined: Aug 11, 2010
Thanks guys! It's much clearer now. So is it safe to assume that static methods cannot overridden? Is it like adding final except that the compiler doesn't let you override the method completely while with static, you can still at least use the method of the upper class? Is my assumption correct?
Find a copy of Java Puzzlers by Joshua Bloch and Neal Gafter; there is a table in there about the different names for having two identifiers the same. In the case of static methods, it is called hiding. It is a confusing practice because you may find yourself using methods declared in different classes on the same object. We also have an FAQ called something like Hiding vs Overriding. I can't seem to get the link to work at the moment, so I shall have to leave you to look for it. Try in the "Java beginners" section.