File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static method" Watch "static method" New topic
Author

static method

sreedhar lak
Ranch Hand

Joined: Feb 26, 2006
Posts: 42
What are the differences between Method hiding and Method overriding?
Static methods cant be overriden,am i correct?
but...


class StaticSup
{
static void m1()
{
System.out.println("from StaticSup");
}
}
class StaticSub extends StaticSup
{
public static void main(String[] args)
{
System.out.println("Hello World!");
StaticSub obj=new StaticSub();
obj.m1();
obj.m2();


}
static void m1()
{
System.out.println("from StaticSub");

}
void m2()
{
System.out.println("from StaticSub m2");
super.m1();

}
}

expected o/p only it is displaynig
---------- Run ----------
Hello World!
from StaticSub
from StaticSub m2
from StaticSup
-----------------------------
How it is possible....


Thanks a lot<br />Learn from yesterday, live for today, hope for tomorrow. The important thing is to not stop questioning
Ashley Waldrop
Greenhorn

Joined: Feb 27, 2006
Posts: 9
StaticSub class is only redifining method "static void m1()" not overriding although it seems like that at first look

StaticSub obj=new StaticSub();
obj.m1();

Is same as
StaticSub.m1();


so should print "from StaticSub"
sreedhar lak
Ranch Hand

Joined: Feb 26, 2006
Posts: 42
Thank you,
If i replace
obj1.m1() and obj1.m2() with
StaticSub.m1() StaticSub.m2() resp.
It is giving compiletime error ,it is fine.
But what i am trying to ask you is that difference and
suppose if i want to test whether static methods are overriden or not what changes should i make in my code.
thanks a lot
Ashley Waldrop
Greenhorn

Joined: Feb 27, 2006
Posts: 9
You can try something like this in previous code to get a clear picture whats going on

StaticSub obj=new StaticSub();
StaticSup sup = obj;
sup.m1();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static method