I.T. Discussion Community!
-Collapse +Expand
Coder
Search Coder Group:

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

Prestwood eMagazine

April 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 11 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 55 months 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 = P1207A1
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
18642
Since 10/31/2008
-
   Contact Us!
 
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

680 People Online Now!!  
Sign In to see who's online now!  Not a member? Join now. It's free!
Show more stats...


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech