File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "this is about static." Watch "this is about static." New topic

this is about static.

Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 32
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: 15041

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 8 API documentation
Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 32
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();
in case of overriding object type define which method to 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 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

Joined: Apr 06, 2010
Posts: 4541

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: 32
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

Joined: Oct 13, 2005
Posts: 45307
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

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

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: 32
Thanks guys! I now understand static variables.
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

Thanks to all for this sharing of information.

MCSA 2003 | Preparing For OCPJP/SCJP6
I agree. Here's the link:
subject: this is about static.
jQuery in Action, 3rd edition