Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use get/set Methods in java [duplicate]

Tags:

I want to know when to use get and set methods(getName,setName ) in my class and when simple classVariable.name = "" instead а = classVariable.getName()

Here is example of class using set and get methods

public class ClassExampe {      String name;     String course;      public String getName ( )     {         return name;     }      public void setName (String studentName)     {         name = studentName;                }      public String getCourse ( )     {         return course;     }      public void setCourse (String studentCourse)     {         course = studentCourse;     } } 

Thanks

like image 714
Jovan Avatar asked Nov 11 '10 16:11

Jovan


People also ask

When to use Get set methods?

Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively.

What is the purpose of settable method?

Python | set() method set() method is used to convert any of the iterable to sequence of iterable elements with distinct elements, commonly called Set. Parameters : Any iterable sequence like list, tuple or dictionary. Returns : An empty set if no element is passed.

Why we use getters setters instead of public variables?

getters and setter can have validation in them, fields can't. using getter you can get subclass of wanted class. getters and setters are polymorphic, fields aren't. debugging can be much simpler, because breakpoint can be placed inside one method not near many references of that given field.

What will happen if getters and setters are made private?

The reason for declaring the getters and setters private is to make the corresponding part of the object's abstract state (i.e. the values) private. That's largely independent of the decision to use getters and setters or not to hide the implementation types, prevent direct access, etc.


2 Answers

Using Getters / Setters vs using Fields

As a rule of thumb:

use the variables directly from the same class (actually from the same .java file, so inner classes are ok too), use Getters / Setters from other classes.

like image 90
Sean Patrick Floyd Avatar answered Sep 29 '22 07:09

Sean Patrick Floyd


The simple rule is: never use direct access (except, of course, when referring to them from inside the class).

  • field access can't be proxied
  • you may want to have some event notification
  • you may want to guard against race conditions
  • expression languages support setters and getters
  • theoretically this breaks encapsulation. (If we are pedantic, setter and getter for all fields also breaks encapsulation though)
  • you may want to perform some extra logic inside the setter or getter, but that is rarely advisable, since consumers expect this to follow the convention - i.e. being a simple getter/setter.
  • you can specify only a setter or only a getter, thus achieving read-only, or write-only access.

Even if this does not happen that you need any of these, it is not unlikely. And if you start with field access, it will be harder to change.

like image 32
Bozho Avatar answered Sep 29 '22 07:09

Bozho