Why do i need to use setProperty to be able to set the value of a variable in my java class
When i can just create the instance of the variable and use the intance to access my variable and assign a value.
Why do i need to use setProperty to be able to set the value of a variable in my java class
When i can just create the instance of the variable and use the intance to access my variable and assign a value.
What you are referring to is Encapsulation. It is one of the corner stones of object oriented programming. You usually want to restrict the ability of other code to mess around with the internal structure of your class.
Imagine a simple example where setting an integer value to a negative value will make your class fail catastrophically. With a setter
in place, you could be able to handle this better, for instance with something like so:
public void setNumber(int number)
{
if (number < 0)
{
number = 1;
}
this.number = number
}
OR (more likely)
public void setNumber(int number)
{
if (number < 0)
{
throw new Exception("Number can't be less than 0");
}
else
{
this.number = number
}
}
As pointed out by Raveesh Sharma in the comment below, this is the reason why all instance variables should be declared as private.
It's a general implementation of of the object-oriented principle of encapsulation. There are a lot of cases when there is no or minimal advantage to using setters. However, there are cases when it can save you from hard to find bugs.
Example
Class Person
Integer age;
....
setAge(Integer age )
{
if (this.age<age || age>120)
{
Throw new RuntimeException("The age: "+age+"is invalid");
}
else {
this.age=age;
}
As the program grows in complexity, this can help you catch errors earlier. Since you may not know ahead of time how a program is going to evolve over time, it's often best to err on the side of caution and use getters/setters to start with. Most IDE's make this fairly painless.
You sure can assigned values by directly accessing the attributes from a given instance but that's definitely against the fundamentals of Java Object Oriented principles.
The best practices in Java OO recommend you to encapsulate the attributes from a class by making them private, and giving access to them through getter/setter.
Applying this principle is one way of reducing coupling between classes.
You don't need to use getProperty()
or setProperty()
(known as getter and setter methods), but it is the preferred way of accessing class-level properties in Java. It is a fundamental principle used in Obejct-Oriented programming, called Encapsulation, which defined which Objects should have access to which Properties, based on ownership, privacy levels, hierarchy in the Object tree, etc.
It also allows you to do more advanced things with your properties, such as making them synchronised. This allows you to make sure order is preserved when accessing a property, so that you don't overwrite values out of order.
All this stuff wasn't obvious to me when I was first learning programming either, but as you learn more about programming the reasons and benefits start to appear and make more sense.