Results 1 to 3 of 3
- 03-26-2010, 08:46 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 49
- Rep Power
- 0
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::
- 03-26-2010, 09:42 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
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...
-
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.
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.Well, please read carefully AND answer me carefully..
Similar Threads
-
Getters and Setters
By lheviathan in forum New To JavaReplies: 4Last Post: 11-02-2009, 01:47 AM -
Constructors, Setter & Getter << need some explaining
By A.M.S in forum New To JavaReplies: 4Last Post: 01-01-2009, 12:03 PM -
Getters and Setters
By Charliestons in forum New To JavaReplies: 10Last Post: 09-12-2008, 10:57 PM -
problem in getter method
By freddieMaize in forum Advanced JavaReplies: 5Last Post: 05-26-2008, 07:02 AM -
How To Add Fields And Generating Getter-Setters
By JavaForums in forum NetBeansReplies: 0Last Post: 07-30-2007, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks