(a)
String name;
public String getName() { return name; }
public void setName(String value) { name = value; }
Ex:
obj.setName("Marc");
if (obj.getName() != "Marc")
throw new Exception("That ain't me!");
(b)
string name;
public string Name {
get { return name; }
set { name = value; } // 'value' is the new value
}
Ex:
obj.Name = "Marc";
if (obj.Name != "Marc")
throw new System.Exception("That ain't me!");
Example 3: Implementing the Name property using the name field: (a) in Java; (b) in C#.
Back to Article