Java using set and get

What are the get and set methods in Java?

Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.

The get and set methods are a part of encapsulation. Let’s first take a look at encapsulation.

Encapsulation

Encapsulation is the process of adding code and data into a single unit. In simple words, different materials are added to make a capsule, which is the same case as encapsulation. Moreover, encapsulation is used to hide sensitive data from the users. A class is an example of encapsulation. You can make all members of the class private (members with sensitive data).

The private member/variables can only be accessed if you work within the specific class. Outside the class, you will need the public set and get methods to update and access the value of the private variable.

  • Set : Sets the value of the private variable.
  • Get : Returns the value of the private variable.

Code

public class Student
private Number rollNo; //you can't access this varible outside the Student Class
>
//setter
public void setRollNo(number id)
rollNo = id;
>
//getter
public string getRollNo()
return rollNo;
>

The set method takes in the parameter and assigns it to variable rollNo. The get method returns the value of variable rollNo.

Источник

Java using set and get

Вопрос, почему мы не задаем в конструкторе класса значение через сеттер (в котором выполнена проверка на валидность), а задаем что-то типа this.value = value (вместо setValue(value)) ?? Ниже написал код в виде примера. Если бы в конструкторе было this.value = value, то при инициализации объекта значением например -3 в конструкторе всё было бы ОК. А сеттер в конструкторе не даст сделать такой глупости.

 public class Main < public static void main(String[] args) < //это значение не вызовет ошибку, пока оно положительное: MyClass myClass = new MyClass(3); //MyClass myClass = new MyClass(-3) //тут будет ошибка! myClass.setValue(4); //это значение не вызовет ошибку //myClass.setValue(-4); //а это вызовет ошибку! System.out.println(myClass.getValue()); >> class MyClass < private int value; public MyClass(int value) < setValue(value); >public int getValue() < return value; >public void setValue(int value) < if (value < 0) < throw new IllegalArgumentException ("Значение value должно быть положительным числом!"); >this.value = value; > > 

Для быстрого создания getter и setter в IntelliJ IDEA выделяете переменную класса и нажимаете alt + insert, во всплывшем окошке можно выбрать что хотите создать.

Интересно, почему в гетере пишут просто return name а можно написать так public String getName() < return this.name; >потому что так просто короче? Или функционал меняется?

Что-то я не совсем понял, даже если я объявил переменную класса private но создал сеттер, то мой объект извне можно все равно изменять, разница лишь в проверке значений. А если я не хочу чтобы мой объект изменяли, то я просто не пишу сеттер? Но смогу ли я сам менять значения объекта?

в конструкторе ведь то же можно указать ограничения при создании объекта?,и еще вопрос в Idea можно изменить название переменной сделав пару кликов,и оно меняется везде в коде вот эта замена это аналог замены через сеттер без потери потерь или там более простая логика и он тупо меняет названия?

Источник

Java Encapsulation

You learned from the previous chapter that private variables can only be accessed within the same class (an outside class has no access to it). However, it is possible to access them if we provide public get and set methods.

The get method returns the variable value, and the set method sets the value.

Syntax for both is that they start with either get or set , followed by the name of the variable, with the first letter in upper case:

Example

public class Person < private String name; // private = restricted access // Getter public String getName() < return name; >// Setter public void setName(String newName) < this.name = newName; >> 

Example explained

The get method returns the value of the variable name .

The set method takes a parameter ( newName ) and assigns it to the name variable. The this keyword is used to refer to the current object.

However, as the name variable is declared as private , we cannot access it from outside this class:

Example

If the variable was declared as public , we would expect the following output:

However, as we try to access a private variable, we get an error:

MyClass.java:4: error: name has private access in Person
myObj.name = «John»;
^
MyClass.java:5: error: name has private access in Person
System.out.println(myObj.name);
^
2 errors

Instead, we use the getName() and setName() methods to access and update the variable:

Example

public class Main < public static void main(String[] args) < Person myObj = new Person(); myObj.setName("John"); // Set the value of the name variable to "John" System.out.println(myObj.getName()); >> // Outputs "John" 

Why Encapsulation?

  • Better control of class attributes and methods
  • Class attributes can be made read-only (if you only use the get method), or write-only (if you only use the set method)
  • Flexible: the programmer can change one part of the code without affecting other parts
  • Increased security of data

Источник

Java using set and get

Как вы заметили, 2-й элемент массива scores изменяется вне сеттера (в строке 5). Поскольку геттер возвращает ссылку на scores, внешний код, имея эту ссылку, может вносить изменения в массив.

Решение этой проблемы заключается в том, что геттеру необходимо возвращать копию объекта, а не ссылку на оригинал. Модифицируем вышеупомянутый геттер следующим образом:

Переменные примитивных типов вы можете свободно передавать/возвращать прямо в сеттере/геттере, потому что Java автоматически копирует их значения. Таким образом, ошибок № 2 и № 3 можно избежать.

 private float amount; public void setAmount(float amount) < this.amount = amount; >public float getAmount()

String — это immutable-тип. Это означает, что после создания объекта этого типа, его значение нельзя изменить. Любые изменения будут приводить к созданию нового объекта String. Таким образом, как и для примитивных типов, вы можете безопасно реализовать геттер и сеттер для переменной String:

 private String address; public void setAddress(String address) < this.address = address; >public String getAddress()

Т.к. объекты класса java.util.Date являются изменяемыми, то внешние классы не должны иметь доступ к их оригиналам. Данный класс реализует метод clone() из класса Object, который возвращает копию объекта, но использовать его для этих целей не стоит.

По этому поводу Джошуа Блох пишет следующее: «Поскольку Date не является окончательным классом, нет га­рантии, что метод clone() возвратит объект, класс которого именно java.util.Date: он может вернуть экземпляр ненадежного подкласса, созданного специально для нанесения ущерба. Такой подкласс может, например, записы­вать ссылку на каждый экземпляр в момент создания последнего в закрытый статический список, а затем предоставить злоумышленнику доступ к этому списку. В результате злоумышленник получит полный контроль над всеми эк­земплярами копий. Чтобы предотвратить атаки такого рода, не используйте метод clone() для создания копии параметра, тип которого позволяет нена­дежным сторонам создавать подклассы».

Источник

Getters and Setters in Java Explained

Getters and Setters in Java Explained

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.

By convention, getters start with the word «get» and setters with the word «set», followed by a variable name. In both cases the first letter of the variable’s name is capitalized:

public class Vehicle < private String color; // Getter public String getColor() < return color; >// Setter public void setColor(String c) < this.color = c; >>

The getter method returns the value of the attribute. The setter method takes a parameter and assigns it to the attribute.

Once the getter and setter have been defined, we use it in our main:

public static void main(String[] args) < Vehicle v1 = new Vehicle(); v1.setColor("Red"); System.out.println(v1.getColor()); >// Outputs "Red"

Getters and setters allow control over the values. You may validate the given value in the setter before actually setting the value.

Why use getters and setters?

Getters and setters allow you to control how important variables are accessed and updated in your code. For example, consider this setter method:

public void setNumber(int number) < if (number < 1 || number >10) < throw new IllegalArgumentException(); >this.number = num; >

By using the setNumber method, you can be sure the value of number is always between 1 and 10. This is much better than updating the number variable directly:

If you update number directly, it’s possible that you’ll cause unintended side effects somewhere else in your code. Here, setting number to 13 violates the 1 to 10 constraint we want to establish.

Making number a private variable and using the setNumber method would prevent this from happening.

On the other hand, the only way to read the value of number is by using a getter method:

Источник

Читайте также:  Generic class field java
Оцените статью