C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Can any one explain me when should I declare a method as static?


Posted Date: 05 Sep 2008      Total Responses: 1

Posted By: Goutham Anand       Member Level: Gold     Points: 1


Can any one explain me when should I declare a method as static?

I know that static method can be called without creating the object for the class. But when should we declare a method to be static? what advantage do we gain technically by declaring a method as static?

Can anyone give me an example where I can't do without declaring a method as static?




Responses

Author: J Ramesh    05 Sep 2008Member Level: SilverRating:     Points: 3
Hi Goutham,

It's a question of design. The answer lies in the purpose of your class.

If you have an utility method like java.lang.Math.max(), it's a good idea to declare that method static. This way, you don't have to instatiate an object every time just for using that method (you don't need that object anyway).

Although a static method can't access instance variables, it can access static variables. A common use of static variables is to define "constants". Examples from the Java library are Math.PI or Color.RED. They are qualified with the class name, so you know they are static. Any method, static or not, can access static variables. Instance variables can be accessed only by instance methods.

Hope this helps for more clarity check the following sites
http://www.experts-exchange.com/Programming/Languages/Java/Q_20183269.html
http://www.leepoint.net/notes-java/flow/methods/50static-methods.html

regards
Ramesh



Post Reply
You must Sign In to post a response.
Next : Can any one explain me when should I declare a method as static?
Previous : How to Transform XML using XSLT in ASP.NET using C#.
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design

web conferencing services

Contact Us    Privacy Policy    Terms Of Use