using a getter and setter function viable for account? The Next CEO of Stack OverflowWhat's the difference between a method and a function?Does functional programming replace GoF design patterns?What is the difference between an abstract function and a virtual function?Why use getters and setters/accessors?What's the pythonic way to use getters and setters?Getter and Setter?org.springframework.orm.hibernate3.HibernateQueryException - HibernateTemplateUsing @property versus getters and settersProperty getters and settersJava conventions for accessible data. (Public accessors & Getters/Naming)

What's the point of interval inversion?

What does "Its cash flow is deeply negative" mean?

WOW air has ceased operation, can I get my tickets refunded?

How can I get through very long and very dry, but also very useful technical documents when learning a new tool?

Science fiction (dystopian) short story set after WWIII

How to start emacs in "nothing" mode (`fundamental-mode`)

Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?

Why does standard notation not preserve intervals (visually)

I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin

How to use tikz in fbox?

What happens if you roll doubles 3 times then land on "Go to jail?"

Was a professor correct to chastise me for writing "Prof. X" rather than "Professor X"?

Why is there a PLL in CPU?

What is meant by a M next to a roman numeral?

Horror movie/show or scene where a horse creature opens its mouth really wide and devours a man in a stables

How do scammers retract money, while you can’t?

How to count occurrences of text in a file?

How easy is it to start Magic from scratch?

Is HostGator storing my password in plaintext?

How can I open an app using Terminal?

The King's new dress

How do I construct this japanese bowl?

Rotate a column

How should I support this large drywall patch?



using a getter and setter function viable for account?



The Next CEO of Stack OverflowWhat's the difference between a method and a function?Does functional programming replace GoF design patterns?What is the difference between an abstract function and a virtual function?Why use getters and setters/accessors?What's the pythonic way to use getters and setters?Getter and Setter?org.springframework.orm.hibernate3.HibernateQueryException - HibernateTemplateUsing @property versus getters and settersProperty getters and settersJava conventions for accessible data. (Public accessors & Getters/Naming)










0















I am doing a program that involve creating an account, I need to create so that it will scan specific data to carry out an assigned command. is the getter and setter function suitable for it?



public class Account {

//data
private int userId;
private String password;
private char type;

public Account(int userId, String password, char type)
this.userId = userId;
this.password = password;
this.type = type;


public int getUserId()
return userId;


public void setUserId(int userId)
this.userId = userId;


public String getPassword()
return password;


public void setPassword(String password)
this.password = password;


public char getType()
return type;


public void setType(char type)
this.type = type;



//methods
public boolean verifyLogin(int usrid , String pass)

if((usrid == userId) & (pass == password))
return true;

else
return false;











share|improve this question



















  • 2





    it wouldn't be logic to change the userId, so you should remove the setUserId, in your verifyLogin, you should compare pass using the equals method instead of the == operator. As for your question, could you please describe more clearly exactly what you are asking?

    – Stultuske
    Mar 8 at 12:51











  • In your implementation getter and setter do absolutely nothing. I do not know what the obsession is for universities to ask encapsulation for all methods. If you would do a check for example, if password matches old password, then do not allow to set it, it makes sense. Otherwise, what is the point of encapsulating it, if you could use direct field access?

    – Atizs
    Mar 8 at 12:54











  • Why do you think it won't be suitable ?

    – vincrichaud
    Mar 8 at 12:58











  • You should use && instead of & in verifyLogin method.

    – Corentin
    Mar 8 at 13:26











  • well it is needed to put those getter and setters for some of the key methods, but you are right in the sense that I shouldent apply encapsulation to all of my methods. i will keep that in mind.

    – Satu Student
    Mar 11 at 13:58















0















I am doing a program that involve creating an account, I need to create so that it will scan specific data to carry out an assigned command. is the getter and setter function suitable for it?



public class Account {

//data
private int userId;
private String password;
private char type;

public Account(int userId, String password, char type)
this.userId = userId;
this.password = password;
this.type = type;


public int getUserId()
return userId;


public void setUserId(int userId)
this.userId = userId;


public String getPassword()
return password;


public void setPassword(String password)
this.password = password;


public char getType()
return type;


public void setType(char type)
this.type = type;



//methods
public boolean verifyLogin(int usrid , String pass)

if((usrid == userId) & (pass == password))
return true;

else
return false;











share|improve this question



















  • 2





    it wouldn't be logic to change the userId, so you should remove the setUserId, in your verifyLogin, you should compare pass using the equals method instead of the == operator. As for your question, could you please describe more clearly exactly what you are asking?

    – Stultuske
    Mar 8 at 12:51











  • In your implementation getter and setter do absolutely nothing. I do not know what the obsession is for universities to ask encapsulation for all methods. If you would do a check for example, if password matches old password, then do not allow to set it, it makes sense. Otherwise, what is the point of encapsulating it, if you could use direct field access?

    – Atizs
    Mar 8 at 12:54











  • Why do you think it won't be suitable ?

    – vincrichaud
    Mar 8 at 12:58











  • You should use && instead of & in verifyLogin method.

    – Corentin
    Mar 8 at 13:26











  • well it is needed to put those getter and setters for some of the key methods, but you are right in the sense that I shouldent apply encapsulation to all of my methods. i will keep that in mind.

    – Satu Student
    Mar 11 at 13:58













0












0








0








I am doing a program that involve creating an account, I need to create so that it will scan specific data to carry out an assigned command. is the getter and setter function suitable for it?



public class Account {

//data
private int userId;
private String password;
private char type;

public Account(int userId, String password, char type)
this.userId = userId;
this.password = password;
this.type = type;


public int getUserId()
return userId;


public void setUserId(int userId)
this.userId = userId;


public String getPassword()
return password;


public void setPassword(String password)
this.password = password;


public char getType()
return type;


public void setType(char type)
this.type = type;



//methods
public boolean verifyLogin(int usrid , String pass)

if((usrid == userId) & (pass == password))
return true;

else
return false;











share|improve this question
















I am doing a program that involve creating an account, I need to create so that it will scan specific data to carry out an assigned command. is the getter and setter function suitable for it?



public class Account {

//data
private int userId;
private String password;
private char type;

public Account(int userId, String password, char type)
this.userId = userId;
this.password = password;
this.type = type;


public int getUserId()
return userId;


public void setUserId(int userId)
this.userId = userId;


public String getPassword()
return password;


public void setPassword(String password)
this.password = password;


public char getType()
return type;


public void setType(char type)
this.type = type;



//methods
public boolean verifyLogin(int usrid , String pass)

if((usrid == userId) & (pass == password))
return true;

else
return false;








java oop getter-setter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 13:09









Yassin Hajaj

14.5k72961




14.5k72961










asked Mar 8 at 12:49









Satu StudentSatu Student

1




1







  • 2





    it wouldn't be logic to change the userId, so you should remove the setUserId, in your verifyLogin, you should compare pass using the equals method instead of the == operator. As for your question, could you please describe more clearly exactly what you are asking?

    – Stultuske
    Mar 8 at 12:51











  • In your implementation getter and setter do absolutely nothing. I do not know what the obsession is for universities to ask encapsulation for all methods. If you would do a check for example, if password matches old password, then do not allow to set it, it makes sense. Otherwise, what is the point of encapsulating it, if you could use direct field access?

    – Atizs
    Mar 8 at 12:54











  • Why do you think it won't be suitable ?

    – vincrichaud
    Mar 8 at 12:58











  • You should use && instead of & in verifyLogin method.

    – Corentin
    Mar 8 at 13:26











  • well it is needed to put those getter and setters for some of the key methods, but you are right in the sense that I shouldent apply encapsulation to all of my methods. i will keep that in mind.

    – Satu Student
    Mar 11 at 13:58












  • 2





    it wouldn't be logic to change the userId, so you should remove the setUserId, in your verifyLogin, you should compare pass using the equals method instead of the == operator. As for your question, could you please describe more clearly exactly what you are asking?

    – Stultuske
    Mar 8 at 12:51











  • In your implementation getter and setter do absolutely nothing. I do not know what the obsession is for universities to ask encapsulation for all methods. If you would do a check for example, if password matches old password, then do not allow to set it, it makes sense. Otherwise, what is the point of encapsulating it, if you could use direct field access?

    – Atizs
    Mar 8 at 12:54











  • Why do you think it won't be suitable ?

    – vincrichaud
    Mar 8 at 12:58











  • You should use && instead of & in verifyLogin method.

    – Corentin
    Mar 8 at 13:26











  • well it is needed to put those getter and setters for some of the key methods, but you are right in the sense that I shouldent apply encapsulation to all of my methods. i will keep that in mind.

    – Satu Student
    Mar 11 at 13:58







2




2





it wouldn't be logic to change the userId, so you should remove the setUserId, in your verifyLogin, you should compare pass using the equals method instead of the == operator. As for your question, could you please describe more clearly exactly what you are asking?

– Stultuske
Mar 8 at 12:51





it wouldn't be logic to change the userId, so you should remove the setUserId, in your verifyLogin, you should compare pass using the equals method instead of the == operator. As for your question, could you please describe more clearly exactly what you are asking?

– Stultuske
Mar 8 at 12:51













In your implementation getter and setter do absolutely nothing. I do not know what the obsession is for universities to ask encapsulation for all methods. If you would do a check for example, if password matches old password, then do not allow to set it, it makes sense. Otherwise, what is the point of encapsulating it, if you could use direct field access?

– Atizs
Mar 8 at 12:54





In your implementation getter and setter do absolutely nothing. I do not know what the obsession is for universities to ask encapsulation for all methods. If you would do a check for example, if password matches old password, then do not allow to set it, it makes sense. Otherwise, what is the point of encapsulating it, if you could use direct field access?

– Atizs
Mar 8 at 12:54













Why do you think it won't be suitable ?

– vincrichaud
Mar 8 at 12:58





Why do you think it won't be suitable ?

– vincrichaud
Mar 8 at 12:58













You should use && instead of & in verifyLogin method.

– Corentin
Mar 8 at 13:26





You should use && instead of & in verifyLogin method.

– Corentin
Mar 8 at 13:26













well it is needed to put those getter and setters for some of the key methods, but you are right in the sense that I shouldent apply encapsulation to all of my methods. i will keep that in mind.

– Satu Student
Mar 11 at 13:58





well it is needed to put those getter and setters for some of the key methods, but you are right in the sense that I shouldent apply encapsulation to all of my methods. i will keep that in mind.

– Satu Student
Mar 11 at 13:58












2 Answers
2






active

oldest

votes


















2














Your getters and setters look fine for accessing the data of this class.



Something you need to be very careful with is how you check if the password is correct.



In your implementation you use pass == password for comparing two strings. This is NOT correct and you should rather use pass.equals(password).






share|improve this answer






























    1














    It looks fine, but you need to rethink if Setters for some values are necessary. In example it is not very common use case that UserID will change somehow. If you want to keep it persistant, setter is not necessary. Set it once in constructor.



    Additionally you can take a look on the Lombok Project and @Getter & @Setter annotation. It will minimalize your code to 3 lines.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063590%2fusing-a-getter-and-setter-function-viable-for-account%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Your getters and setters look fine for accessing the data of this class.



      Something you need to be very careful with is how you check if the password is correct.



      In your implementation you use pass == password for comparing two strings. This is NOT correct and you should rather use pass.equals(password).






      share|improve this answer



























        2














        Your getters and setters look fine for accessing the data of this class.



        Something you need to be very careful with is how you check if the password is correct.



        In your implementation you use pass == password for comparing two strings. This is NOT correct and you should rather use pass.equals(password).






        share|improve this answer

























          2












          2








          2







          Your getters and setters look fine for accessing the data of this class.



          Something you need to be very careful with is how you check if the password is correct.



          In your implementation you use pass == password for comparing two strings. This is NOT correct and you should rather use pass.equals(password).






          share|improve this answer













          Your getters and setters look fine for accessing the data of this class.



          Something you need to be very careful with is how you check if the password is correct.



          In your implementation you use pass == password for comparing two strings. This is NOT correct and you should rather use pass.equals(password).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 13:16









          JonaswgJonaswg

          1559




          1559























              1














              It looks fine, but you need to rethink if Setters for some values are necessary. In example it is not very common use case that UserID will change somehow. If you want to keep it persistant, setter is not necessary. Set it once in constructor.



              Additionally you can take a look on the Lombok Project and @Getter & @Setter annotation. It will minimalize your code to 3 lines.






              share|improve this answer



























                1














                It looks fine, but you need to rethink if Setters for some values are necessary. In example it is not very common use case that UserID will change somehow. If you want to keep it persistant, setter is not necessary. Set it once in constructor.



                Additionally you can take a look on the Lombok Project and @Getter & @Setter annotation. It will minimalize your code to 3 lines.






                share|improve this answer

























                  1












                  1








                  1







                  It looks fine, but you need to rethink if Setters for some values are necessary. In example it is not very common use case that UserID will change somehow. If you want to keep it persistant, setter is not necessary. Set it once in constructor.



                  Additionally you can take a look on the Lombok Project and @Getter & @Setter annotation. It will minimalize your code to 3 lines.






                  share|improve this answer













                  It looks fine, but you need to rethink if Setters for some values are necessary. In example it is not very common use case that UserID will change somehow. If you want to keep it persistant, setter is not necessary. Set it once in constructor.



                  Additionally you can take a look on the Lombok Project and @Getter & @Setter annotation. It will minimalize your code to 3 lines.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 at 13:27









                  Adam MacierzyńskiAdam Macierzyński

                  209112




                  209112



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063590%2fusing-a-getter-and-setter-function-viable-for-account%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                      Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                      2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived