0

so I have been trying to understand what property exactly mean. I have searched for previously asked Q/A in stackoverflow and other website but the answers that I came across were not specific as to whether fields(instance variables) that are modified with setters and getters are also called properties.

The definition I came across was "a combination of setters and getters methods that modify fields of an object"

Below is just a small piece of code to make you understand my question better if you need more clarification.

//property?
String name;

//property?
public void setName(String n){
    name = n;
}
//property?
public String getName(){
    return name;
} 
aziz
  • 83
  • 1
  • 7
  • I heard of them as *class members* or *class attributes*, non static ones of course if they are private and thus not modifiable without getters and setters. Maybe *Property* is a legal description... There is also a class `java.util.Properties` in java extending `HashTable`. – deHaar Jun 25 '18 at 12:13
  • @deHaar don't confuse `java.util.Properties`, which is a class in the `Java API`, with properties of an object (or class, or other entity) , that is an important general concept in object-oriented domain analysis, design and development, and does not pertain to a specific language. They are completely different things that have no relation to each other (except that `java.util.Properties` has its own propweries :) ). `java.util.Properties` is a class that helps store various named values (like software settings) to a file and read them afterwards. – m. vokhm Jun 25 '18 at 13:17
  • @m.vokhm I did not want to confuse anybody by mentioning `java.util.Properties`. I found it worth mentioning there is a class called `Properties` to make sure noone gets confused by that when talking about general concepts and their terminologies. – deHaar Jun 25 '18 at 13:21
  • @deHaar And BTW properties may be static (class properties) as well. They can be static, dynamic, private, public, protected, final and whatever else one could imagine. – m. vokhm Jun 25 '18 at 13:26
  • @m.vokhm sure, but the question asked about *properties* modifiable via getters and setters only, that makes them private at least, doesn't it? – deHaar Jun 25 '18 at 13:35
  • @deHaar No, the qustion was *'what property exactly mean'*. It does not mean **only** properties modifiable via setters and acessible via getters. – m. vokhm Jun 25 '18 at 13:40

2 Answers2

0

Properties means any members that belongs to the class. It could be variable, objects of other/ same class, methods of that class etc.

basically getter/setter are used for those member variables only.

Local variables are properties of that method that it belongs to and not property of the class.

Shubhendu Pramanik
  • 2,711
  • 2
  • 13
  • 23
  • so how come if you search for the definition of property in this website, they define it as "a combination of setters and getters methods that modify fields of an object"? – aziz Jun 25 '18 at 12:17
  • @aziz read more abut `getter/ setter` here: https://stackoverflow.com/questions/10407877/what-is-the-point-of-getters-and-setters – Shubhendu Pramanik Jun 25 '18 at 12:25
0

In the OOP world, "property" has a rather broad sense and its specific meaning depends on the context. Generally, it is an attribute of an entity; ith may be a name or an age of a person, a color of a flower, a height of a building etc. A property has its name and its value (e.g. flower.color = red -- here color is the name, and red is the value), the value may belong to different types (or classes): a string, a number, a person, an enterprise... It may have a constant value (that never change during the lifetime of the owner (the entity it belongs to)) or it may have a variable value that can be changed by the user. In the software area it can be talked about at a conceptual level of the domain analysis and the software design; in this case people usually don't care how exactly it would be implemented. As well, it may be used at the level of concrete implementation (program code), and then the means to implement this concept depend on the programming language.

In Java, for example, when we say 'property' we usually mean a field (variable) of an object and a couple of methods (or a single method for read-only properties) to access its value (getter and setter):

class Person {
  private String name;         // the field to hold the value
  public Person(String name) { // Constructor
    this.name = name // The name is given at the moment it's been born 
  }
  public String getName() { return Name; } // getter
  // No, name can't be changed after it's been born -- it's a read-only property, thus no setter
  // public void setName(String name) { this.name = name; } // otherwise the setter would look like this
}

In such a case, a user can acces the value of the property with the following code:

 System.out.println(thisPerson.getName());

Other languages (like C#, for example) have means to code properties in somewhat more convenient way:

class AnotherPersonType {
  private string name // a field to hold the value  
  public string Name 
  {
     get => name;         // getter, the same as "return this.name;" 
     set => name = value; // setter, the same as "this.name = value;"
  }    
}  
.....
anotherPerson.name = "John"; // It looks like an assignment, 
                              // but in fact, the setter is invoked   
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
m. vokhm
  • 669
  • 1
  • 6
  • 24