Also known as a Class Field.
A class variable defined with a specific class visibility, usually private visibility. A member property is different than a member field. A member property uses a member field to store values through accessor methods (getters and setters). For example, it is common to use a private member field to store the current value of a property. The current values of all the class member fields is the current state of the object.
Languages Focus What modifiers apply to member fields, if any? Typical member field modifiers include scope modifiers (private, protected, etc.) and read-only. Can you initialize the value of a member field when declared ensuring a default value?
VB.Net:
In VB.Net you can set the visibility of a member field to any visibility: private , protected , public , friend or protected friend .
You can intialize a member field with a default when declared. If you set the member field value in your constructor, it will override the default value.
Finally, you can use the Shared modifier (no instance required) and ReadOnly modifier (similar to a constant).
Syntax Example: Public Class Cyborg Private FSerialNumber As String = "A100" Public CyborgName As String Public CyborgAge As Integer Public Shared ReadOnly SeriesID As Integer = 100 End Class
Cross Reference Examples:
ASP Classic:
ASP Classic does support member fields, but, as usual, you cannot initialize the type nor value of a member field. The type is implied by usage.
Syntax Example:
Class Cyborg Private FSerialNumber Public FCyborgName Public FCyborgAge Public FSeriesID End Class
C#:
In C# you can set the visibility of a member field to any visibility: private , protected , public , internal or protected internal .
You can intialize a member field with a default when declared. If you set the member field value in your constructor, it will override the default value.
Finally, you can use the static modifier (no instance required) and readonly modifier (similar to a constant).
Syntax Example: public class Cyborg : System.Object { private string serialNumber = "A100" ; public string cyborgName; public int cyborgAge = 0; public static readonly int seriesID = 100; }
Corel Paradox:
Not Supported
Delphi:
In Delphi, it is common to start all member fields with "F" as in FName and FAge . You can initialize the value of member fields too.
Delphi member fields do not support static data. The workaround is to use the hybrid nature of Delphi and use a unit variable (a variable declared in the implementation section of a unit) and then access the unit variable with a member property.
Delphi doesn't support setting a member field to read-only. However, you can accomplish the task with a strict private member field and a read-only property.
Syntax Example: TCyborg = class(TObject) private FSerialNumber: String='A100'; public FCyborgName: String; FCyborgAge: Integer=0; FSeriesID: Integer=100; end;
Delphi Prism:
In Prism you can set the visibility of a member field to any visibility: private , protected , public , assembly and protected or assembly or protected .
Prism supports the readonly modifier for member fields which is handy for constant like data. In this case, I chose not to preface my read-only member field with "F" so it's usage is just like a read-only property.
Prism also support the class modifier (static data) for member fields.
Delphi developers should notice the use of := to initialize a member field (in Delphi you use an = ).
Syntax Example: Cyborg = class(System.Object)
private
FSerialNumber: String:="A100" ;
public
FCyborgName: String;
FCyborgAge: Integer:=0;
class SeriesID: Integer:=100; readonly;
end;
Java:
In Java, you can set the scope of a field member to public , protected , or private . Additional modifiers are static , abstract , final (assign only once), strictfp (strict floating point values) transient (do not save to persistent storage), and volatile (all threads see same value).
You can initialize member fields as in:
int age = 0;
More Info / Comment