IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Coder
Search Coder Group:

Advanced
-Collapse +Expand Coder Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Coder Store
PRESTWOODSTORE

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBRole-Based T...Coding & OOObject Orien...   Print This     
  From the October 2015 Issue of Prestwood eMag
 
Coder Object Orientation (OO):
Static Class / Static Member
 
Posted 16 years ago on 10/31/2008 and updated 1/20/2009
Take Away:

A static member is a member you can have access to without instantiating the class into an object. For example, you can read and write static properties and call static methods without ever creating the class. Static members are also called class members (class methods, class properties, etc.) since they belong to the class and not to a specific object. A static class is a class that contains only static members. In the UML, these classes are described as utility classes.

Coder Definition Flashcard:
 A flashcard from our Coder Flashcards Library
 A definition from our Coder Definitions Page

KB101477

Static Class / Static Member -

A static member is a member you can have access to without instantiating the class into an object. For example, you can read and write static properties and call static methods without ever creating the class. Static members are also called class members (class methods, class properties, etc.) since they belong to the class and not to a specific object. A static class is a class that contains only static members. In the UML, these classes are described as utility classes.


Static Methods

A.k.a. Static Member

Depending on the lanaguage, you can make any class member static (methods, fields, properties, and even events). Using static methods is similiar to using global functions and data in languages that support global functions and data (like Object Pascal and C++). In languages that don't support global functions and data, you may end up using a lot of static methods and static data.

Calling Static Members

When calling a static method, you do so through the class itself and not through the object instantiated. Typically you use ClassName.StaticMethodName. If you use the ObjectInstanceName.StaticMethodName, you will either get an error or the language will allow the usage but the compiler will use the class even though you specified the object. For example, C# and VB.Net require the class name, but with Java you can use either the class  or object name. However, with languages that allow you to use the object instance, it is strongly recommended that you don't because of the potential confusion.

Static Fields

In general, any method (static or not) can access a static field. However, a static method cannot access it's own non-static member fields.

Why use static methods

  • Faster Code - Many compilers will optimize static methods because no object instance is involved (passed).
  • Less Source Code - Since you don't have to create an object instance, you will have less lines of code.
  • Documentation - Since static methods are not associated with an object instance, it is clear when looking at code that uses the syntax ClassName.Method that it is a static method associated with the class and not associated with a particular object instance.

Class vs. Object Data Rule of Thumb

The data used by static methods is the same for all objects because it is stored up at the class level. Object data is unique for each object instance (and represents the current state of the object). One interesting rule of thumb that is somewhat popular is, "Always use static methods UNLESS you need the data to be different for each object instance." Like all rules of thumb, it's interesting because it stresses a particular point or feature. Don't over use this rule of thumb but it is interesting and does bring up several design discussions such as, "If a method doesn't manipulate an object instance's attributes, does the method really belong to this class?"

Static Classes

Use a static class to organize your static methods not associated with particular objects. A static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods. Consider grouping your static methods in static classes for organization. In non-OOP languages it is common to group functions in a way that organizes the routines such as strings, math, etc.

More Info

Code:  ASP Classic Static Members (Not Supported)
Code:  C# Static Members (static)
Code:  C++ Static Members (static)
Code:  Delphi Class Members (Class)
Code:  Delphi Prism Class Members (static)
Code:  Java Static Members (static)
Code:  VB.Net Shared Members (Shared)

Comments

1 Comments.
Share a thought or comment...
Comment 1 of 1

Small correction in:

A static member is a member you can have access to without instantiating the class into an object. For example, you can read and write static properties and call static methods without ever creating the class

i think should be:
... without ever creating the object.

---
Marian Chovan
Posted 10 years ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P161A1
Enter key:
Definition Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101477 Counter
23802
Since 10/31/2008
Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]