aspose file tools*
The moose likes Beginning Java and the fly likes Static Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Method" Watch "Static Method" New topic
Author

Static Method

Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
Hello Ranchers,
In Java one can invoke static method on instance , but in C# .net it gives error & forces us to invoke it on Classname only.

Why it is so?
Is there any violation of OOPs in either case?
Shrinivas
Jerret Halter
Ranch Hand

Joined: Feb 03, 2006
Posts: 62
Coming directly from Sierra and Bats SCJP and SCJD book (ISBN 0-07-222684-6).
This is merely a syntax trick to let you use an object reference variable (but not the object it refers to) to get to a static method or variable, but the static member is still unaware of the particular instance used to invoke the static member.


The book is quite good I used it to pass my SCJP book. You can find it here: SCJP

[ March 29, 2006: Message edited by: Jerret Halter ]
[ March 29, 2006: Message edited by: Jerret Halter ]

Jerret Halter<br /> <br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.<hr></blockquote>
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
I'm with C#. Nothing to do with violation of OO, in my opinion, but the C# style is more clear.

Static memebers are not a part of an object and it makes sense to prohibit the use of calling them using objects.

Allowing to call the static methods on objects can be misleading. Consider the following piece of code



A programmer, who doesn't have the understading of the effect of trying to override a static method, might write a code like this

There is a possibility that the author of the Client class is thinking that he is calling the method defined in the Child class, but actually, it is the Parent version that will be called (as static methods can be hidden, but can't be overridden)

This can be avoided, by making it mandatory to call static methods only on classes and not on instances.



Mani
Quaerendo Invenietis
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Method