PDA

Click to See Complete Forum and Search --> : [RESOLVED] static vs non static


popskie
May 23rd, 2006, 02:42 AM
Hi,
What is differences between a static function and non static? Except that static function need not to instanciate. Any idea or input?

Thanks,

Popskie

penagate
May 23rd, 2006, 03:02 AM
No, that is the difference :)

Like you said static function in a class does not require an instance of the class, you just qualify it with the class name and call it.

Also there is obviously no this pointer available as that would require a class instance. Usually you'd pass an instance to the function, like any of the static functions in the String class for example.

popskie
May 23rd, 2006, 08:16 PM
In memory consumption? If thier is no difference why static keyword evolved in so many programming languages?

jmcilhinney
May 23rd, 2006, 08:44 PM
The use of the "static" keyword is not just a convenience so you don't have to create an instance. There is a conceptual difference between static members and non-static members that too many people seem to ignore. Logically a static member is a member of the class itself, while a non-static member is a member of a specific instance. Take the System.IO.File class for instance. All of its members are static because they all do things that relate to files in the file system, but not to any specific File object within your application. Also take the String class. It has members like Format and Join that do not act on any single specific instance so logically they should be, and are static members. Other members like IndexOf or Split do act on a specific instance so logically they should be, and are, instance members. Generally speaking any method could be made static or not, but one choice is almost always logically more sound than the other.

popskie
May 23rd, 2006, 09:37 PM
thanks penagate and jm for the info.