#41 Getters and Setters in Java

preview_player
Показать описание
Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

Udemy Courses:

For More Queries WhatsApp or Call on : +919008963671

In this lecture we will learn:
Setters and getters in Java
- What are setters?
- What are getters?
-
Shortcut for writing getters and setters in Eclipse:-

- The name of a method should define the objective of the model.
- For every variable, we have two things: Setters and Getters methods inside a class in Java programming.
- Getter and Setter are methods used to protect your data and make your code more secure.
- Getter and Setter make the programmer convenient in setting and getting the value for a particular data type.
- In both getter and setter, the first letter of the variable should be capitalised.

Getters:-
- If you want to get the value of a method, we use getters.
= public int getAge();

Setters:-
- To set the value in a method, we use overloading.
= public void setAge()

- There is no complusion that there should be all methods and variables inside the class.

Shortcut for writing getters setters in Eclipse:-
= Right-click on the editor - source action or insert code - Generate getters and setters.

More Learning :

Donation:
PayPal Id : navinreddy20
Рекомендации по теме
Комментарии
Автор

keep uploading sir, this is very useful!

sakshibhosale
Автор

Can I do this instead?

public void setCharacteristics(int age, String name) {
this.age = age;
this.name = name;
}

broyea
Автор

Is it okay for setters to have more than one line of code? I'm new to java and I'm taking inputs with a scanner in my setter and validating it before the line "this.field = whatever;"

alhassirakhdugani
Автор

sir i have a doubt regarding private and public . we are protecting data by variable then why we need to give public in getter setter method
can u explain please sir?

adithyavarma
Автор

sir i have one doubt
setters are used for set the data corresponding variable
incase of constructor also do a same job you can also
set the values through constructor, then why we should use settrs

balajia
Автор

Whats the need for getter and setter, when the only thing we're doing there is returning or setting a value, why not just make variable public if this is the only thing we do in getter/setter, unnecessary boiler plate,

Getter/Setter makes sense only when we have some validation or other logic in that, otherwise its just boilerplate

vaibhavjaiswal