I am a beginner in Java.I recently came across it in java.I am trying to making a login form and I am trying to use get and set for it.I have classes for username And password verification I also have a registration class to register the user. In which classes and how should I use get and set?
-
Before learning any OOP language, OOPS concept should be clear. For this getter setter problem this link could be useful for you:-http://stackoverflow.com/questions/8830772/why-are-getter-and-setter-method-important-in-java – Deepanshu Jul 18 '16 at 09:52
3 Answers
As the name states
GET
methods (Called getters) are used to return current value of class instance variables
SET
methods (Called setters) are used to set new value of class instance variables
For example:
Class Person {
private String name=null;
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
}
EDIT
Here are Stack Overflow Links to help you
-
Can you please tell the advantages of get and set over normal methods for passing a variable to a class. – ath j Jul 18 '16 at 10:57
GET and SET are methods that can be invoked by an object the set prefix is usually used in methods that set a value to an object's attribute and the get method does the opposite of that it gets the value of the attribute that you have set on the object.
-
Can you please tell the advantages of get and set over normal methods for passing a variable to a class. – ath j Jul 18 '16 at 10:56
-
well there are methods that uses an entire object as a parameter for example a class User that has a username and password attribute can be passed on the loginAuthentication(User objectName) method which has a User class parameter. and it would be more convenient the more parameters that needs to be passed, the GET and SET method are one of the many good coding techniques of OOP(object oriented programming) that makes it easier for other people or yourself to maintain and understand your code's flow. – Kevin Isaac Cortez Jul 19 '16 at 03:23
getter and setter methods in Java help you achieve Encapsulation which is fundamental concept of Object Oriented programming. You can achieve this by declaring private data members while making setter and getter methods public.
In a login form, you have username and password. You will pass them to java class where you can set/get the value of these data members for the current value being passed. For Example,
class Login{
private String username;
private String password;
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return username;
}
public void setPassword(String password){
this.password= password;
}
public String getPassword(){
return password;
}
}
In your validation LoginForm validation class, you can use the setter and getter method instead of getting the values from the request parameter for example as req.getParameter("username"). Not only in your validate class but also, when ever you need to change or get username and password parameters, you can use getter and setter methods.

- 55
- 2
- 11
-
Can you please tell the advantages of get and set over normal methods for passing a variable to a class. – ath j Jul 18 '16 at 10:56
-
This is explained well here [link](http://stackoverflow.com/questions/1461598/what-is-the-point-of-setters-and-getters-in-java). Purpose of using get is to get current instance of the data member and set is for setting a new value to the data member. Unlike normal methods, SET and GET is to have controlled accessibility of your data member.You can also check [here](http://stackoverflow.com/questions/6638964/set-and-get-methods-in-java) – nam Jul 18 '16 at 11:05