Results 1 to 3 of 3
  1. #1
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Question help me with getter and setters in class

    Well, please read carefully AND answer me carefully..

    this is written in a class called "Paycheck"
    it is a setter method.
    public void setCheckNum(String checkNum) {
    this.checkNum = checkNum;
    }

    now i want to refuse any checkNumber != 8 digits...
    how can i do it ??
    and do i have to make any changes in the constructor ? or here in the setter method,,,
    The idea is that i dont want to create an object if checkNum != 8 digits
    Note :ALL data fields are private ..
    please help !!:confused::confused::

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Think of it this way: can you EVER have a check with no check number, or with a wrong check number?
    If your answer is "never", then you have to modify the constructor so that the check number is passed into it, and kill the setter.
    If your answer is "sometimes", then keep the setter.

    now to find out whether there're 8 digits in the number, do checkNum.length() == 8...

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by javanew View Post
    this is written in a class called "Paycheck"
    it is a setter method.
    public void setCheckNum(String checkNum) {
    this.checkNum = checkNum;
    }

    now i want to refuse any checkNumber != 8 digits...
    how can i do it ??
    and do i have to make any changes in the constructor ? or here in the setter method,,,
    The idea is that i dont want to create an object if checkNum != 8 digits
    Note :ALL data fields are private ..
    please help !!:confused::confused::
    If you don't want to create an object, then as noted above, you'll probably have to have a single constructor for the class and have it take a checkNum parameter. If it is not valid, then have the constructor throw an exception.



    Well, please read carefully AND answer me carefully..
    Sorry, but to get this level of service, you'll have to upgrade to the forum's platinum plan. Please have your credit card ready.

Similar Threads

  1. Getters and Setters
    By lheviathan in forum New To Java
    Replies: 4
    Last Post: 11-02-2009, 01:47 AM
  2. Replies: 4
    Last Post: 01-01-2009, 12:03 PM
  3. Getters and Setters
    By Charliestons in forum New To Java
    Replies: 10
    Last Post: 09-12-2008, 10:57 PM
  4. problem in getter method
    By freddieMaize in forum Advanced Java
    Replies: 5
    Last Post: 05-26-2008, 07:02 AM
  5. How To Add Fields And Generating Getter-Setters
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 07-30-2007, 11:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •