What is the use of Getters and Setters method ?

Answers were Sorted based on User's Feedback



What is the use of Getters and Setters method ?..

Answer / marshallsudhan

Before get into the answer, we gotta know something
prior...! "JavaBeans".
JavaBeans are java classes that have properties. For our
purpose, think of properties as private instance variables.
since they're private, the only way they can be accessed
from outside of their class is through 'methods'in the
class.
The methods that change a propertiy's value are called
setter methods, and the methods that retrieve a property's
value are called getter methods.

Is This Answer Correct ?    54 Yes 8 No

What is the use of Getters and Setters method ?..

Answer / sumit bansal

you can put verification code in setter as if you want a
variable for storing weight you never want weight -ve So
write the code that prevent -ve entry of varible value in
setter method

Using getter and making your variable private you can be
sure that nobody can access it directly from outside.

Is This Answer Correct ?    31 Yes 19 No

What is the use of Getters and Setters method ?..

Answer / adarsh gowda

Getter and setter methods are used to retrieve and manipulate private variables in a different class. A "getter" method does as it name suggest, retrieves a the attribute of the same name. A setter method allows you to set the value of the attribute.

Is This Answer Correct ?    3 Yes 1 No

What is the use of Getters and Setters method ?..

Answer / amol patil

That means you should use set<yourPropertyName> for methods
that can modify
a property (normally a property maps directly to an instance
variable, but not
necessarily) and get<yourPropertyName> for methods that can
read a property.
For example, a String variable name would have the following
getter/setter methods:

Is This Answer Correct ?    2 Yes 5 No

Post New Answer

More Core Java Interview Questions

what is hashmap& hashtable with example?

1 Answers   CTS,


How does queue work in java?

1 Answers  


What happens if a constructor is declared private?

1 Answers  


What's the base class in java from which all classes are derived?

1 Answers  


How is Object Oriented Programming different from Procedure Oriented Programming?

1 Answers   Amdocs,


In a container there are 5 components. I want to display the all the components names, how will you do that one?

1 Answers  


Is minecraft 1.15 out?

0 Answers  


Can we make main() thread as daemon?

1 Answers  


Is an integer an object?

1 Answers  


What are the advantages of user defined functions?

1 Answers  


How do you check if a string is lexicographically in java?

1 Answers  


What happens to a static var that is defined within a method of a class?

1 Answers  


Categories