my dog learned polymorphism*
The moose likes Beginning Java and the fly likes this is about static. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "this is about static." Watch "this is about static." New topic
Author

this is about static.

Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 30
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Methods that are static are class members rather than instance members. The page Understanding Instance and Class Members in The Java Tutorials explains it all in detail.

Note that static has a totally different meaning in Java than it has in C.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 30
Hi Jesper,

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.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

X myX = new Y();
myX.getString()
in case of overriding object type define which method to call....here object is of type Y() so class Y method will be called
Y's method
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


SCJP6.0,My blog Ranchers from Delhi
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

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.
Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 30
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?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Francis Zabala wrote:So is it safe to assume that static methods cannot overridden?

yes, mean while you can redefined in subclass *unlike final method*
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
Francis Zabala wrote:Is my assumption correct?
As far as I can tell, no.

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
You will find that FAQ here, at No 10.
Hari haran Ravi
Greenhorn

Joined: Aug 11, 2010
Posts: 26
What Mathew Brown told is correct upto the core. I concur with him
Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 30
Thanks guys! I now understand static variables.
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

Shanky,Matthew,Ritchie,.....;
Thanks to all for this sharing of information.


MCSA 2003 | Preparing For OCPJP/SCJP6
 
wood burning stoves
 
subject: this is about static.
 
Similar Threads
Static initializer order
Overide Question
doubt in ++ operator
Static????????????
SCJP Mock Question : static