IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
   ► KBTo/From GuidesReferenceOOP Details  Print This     

Static Member (Cross Ref > OOP Details)

Static Member

General Info: 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.

Languages Focus

Languages that support static members usually at least support static member fields (the data). Some languages also support static methods, properties, etc. in which case the class member is held in memory at one location and shared with all objects. Finally, some languages support static classes which usually means the compiler will make sure a static class contains only static members.

Access VBA:  "Static Members" Not Supported

ASP Classic:  "Static Members" Not Supported

Although ASP Classic supports the creation of simple classes, it does not support static methods.

More Info

Code:  ASP Classic Static Members (Not Supported)
Definition:  Static Class / Static Member

C#:  "Static Members" static

C# supports both static members and static classes using the static keyword. You can add a static method, field, property, or event to an existing class. Also, you can designate a class as static and the compiler will ensure all members in that class are static. You can add a constructor to a static class to initialize values.

The CLR automatically loads static classes with the program or namespace.

Syntax Example:
//Static Class Example
public static class MyStaticClass
{
  //Static Method Example
  public static void MyStaticMethod()
{
// static method code
}
}


More Info

Code:  C# Static Members (static)
Definition:  Static Class / Static Member

C++:  "Static Members" static

C++ supports static methods and static member fields.

Syntax Example:
class MyUtils {
public:
static void MyStaticMethod();
};

 

More Info

Code:  C++ Static Members (static)
Definition:  Static Class / Static Member

Corel Paradox:  "Static Members" Not Supported

Delphi:  "Class Members" Class

Object Pascal supports static methods, but not static member fields. For static member fields, use traditional Pascal-like global variables.

Since Object Pascal is a hybrid language, you can use global functions and data so the need for class methods is diminished but still useful. For example, since Object Pascal does not have automatic reference counting, you could use a class method to keep track of the number of object instances.

Delphi 1-7: All classes in a unit are friendly (see eachother's private members), some developers like to put each class in it's own unit and reserve putting multiple classes in the same unit until they wish to implement friendly classes.

Delphi 2005+: New strict keyword allows you to indicate friendly.

Delphi Prism: The Strict keyword was introduced from the beginning (Delphi.Net preview that shipped with D7).

Syntax Example:
type
TMyUtils = class(TObject)
public
class function MyStaticMethod: Integer;
end;

In implimentation:

class function TMyUtils.MyStaticMethod: Integer;

More Info

Code:  Delphi Class Members (Class)
Definition:  Static Class / Static Member

Delphi Prism:  "Class Members" static

In Prism, you static members are also referred to as class members. You implement class members using the static keyword.


More Info

Code:  Delphi Prism Class Members (static)
Definition:  Static Class / Static Member

Java:  "Static Members" static

When calling a static method from within the same class, you don't need to specify the class name.

Syntax Example:
class MyUtils {
//Static method.
  public static void MyStaticMethod() {
}
}



Static Method / Static Class
A static method is a method you can call without instantiating the class. Also called a class method since it belongs to the class and not to a specific object created from the class. A static class is a class that contains only static members. In the UML, these classes are described as utility classes.

More Info

Code:  Java Static Members (static)
Definition:  Static Class / Static Member

VB Classic:  "Static Members" Not Supported

VB.Net:  "Shared Members" Shared

VB.Net supports both static members and static classes (use the keyword Shared). You can add a static method, field, property, or event to an existing class.

You can designate a class as static and the compiler will ensure all methods in that class are static. You can add a constructor to a static class to initialize values.

The CLR automatically loads static classes with the program or namespace.

Syntax Example:
Public Shared Function MySharedFunction() As Integer
  MySharedFunction = 12345
End Function




Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Advanced

1 Advanced Level Question

Question #1: Multiple Choice

Which answer best describes VB.Net's support for static members and clsasses?

Answer:
1. 

VB.Net supports both static members and static classes (use the keyword Static). You can add a static method, field, property, or event to an existing class.

2. 

VB.Net supports both static members and static classes (use the keyword Shared). You can add a static method, field, property, or event to an existing class.

3. 

VB.Net supports static members only (use the keyword Shared).

4. 

VB.Net supports static classes only (use the keyword Static).

5. 

None of the above. VB.Net does not support static classes nor members.

More Info

Definition:  Static Class / Static Member
Code:  VB.Net Shared Members (Shared)




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


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