Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > May I do data type conversion within getter/setter?

Reply
Thread Tools

May I do data type conversion within getter/setter?

 
 
Raoul Markus
Guest
Posts: n/a
 
      09-20-2003
Hi,

I had to change the type of in private variable (from java.util.Date to
java.sql.Date). May I do type conversions within the getters and setters to
let the outside interface stay untouched?
i.e.

private java.sql.Date mDate;

public java.util.Date getDate() {
return new java.util.Date(mDate.getTime());
}

 
Reply With Quote
 
 
 
 
Juha Laiho
Guest
Posts: n/a
 
      09-20-2003
Raoul Markus <(E-Mail Removed)> said:
>I had to change the type of in private variable (from java.util.Date to
>java.sql.Date). May I do type conversions within the getters and setters to
>let the outside interface stay untouched?


Yep; that (being able to modify the class internals without needing to
modify anything outside the class) is one of the reasons you have getters
and setters instead of just providing public access to the variables.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
 
Reply With Quote
 
 
 
 
David Zimmerman
Guest
Posts: n/a
 
      09-20-2003


Raoul Markus wrote:
> Hi,
>
> I had to change the type of in private variable (from java.util.Date to
> java.sql.Date). May I do type conversions within the getters and setters to
> let the outside interface stay untouched?
> i.e.
>
> private java.sql.Date mDate;
>
> public java.util.Date getDate() {
> return new java.util.Date(mDate.getTime());
> }
>

That's one of the best reasons to use getters/setters. When the
internal data needs to change but you don't want the external API to change.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-20-2003
On Sat, 20 Sep 2003 19:38:26 +0200, Raoul Markus <(E-Mail Removed)>
wrote or quoted :

>I had to change the type of in private variable (from java.util.Date to
>java.sql.Date). May I do type conversions within the getters and setters to
>let the outside interface stay untouched?


yes. Further there is nothing to do.

you can write:

private java.sql.Date d;

public java.util.Date getDate()
{
return d;
}

Since java.sql.Date is a subclass of java.util.Date.

Most people would not even mind if you wrote:

public java.sql.Date getDate()
{
return d;
}

Since they can use the result as a java.util.Date without conversion.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BayPIGgies: May *THIRD* Thursday at Google (May 19) Aahz Python 0 04-22-2005 11:59 PM
News Proxy may be why many people may have missed the vote Renee Digital Photography 5 10-27-2004 06:02 AM
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. luna ASP .Net 1 02-13-2004 01:15 PM
How to optionally use classes that may or may not be installed ? Sam Iam Java 0 01-31-2004 04:09 AM
how may in hell may i take advantage of a IF statement in two separate functions? like quit anytime with button Q Rahmi Acar C++ 5 07-28-2003 08:14 AM



Advertisments