Subject: Var access question... 2006-11-01 - By Karr, David
Back I often implement private getters for properties that should only be accessed from within the class. I like to keep local variables and instance variables semantically apart, to emphasize that instance variables should only be declared if they're really necessary.
> -- --Original Message-- -- > From: A mailing list for Java(tm) 2 Platform, Enterprise > Edition [mailto:J2EE-INTEREST@(protected)] On Behalf Of Ne'Bahn > Sent: Tuesday, October 31, 2006 4:59 AM > To: J2EE-INTEREST@(protected) > Subject: Var access question... > > Hi list, I'm wondering if it's a good practice to access > fields (in the same class) through its names and not by its > accessors, e.g. > > public class ComplexNumber { > private double U; > private double V; > > // constructor deleted > > public double real() { > return U; > } > > public double imaginary() { > return V; > } > > public double magnitude() { > return (Math.sqrt(Math.pow(U, 2) + Math.pow(V, 2))); > // access here must be U or real(), same with V } > > public double arg() throws ArithmeticException { > return (Math.atan(V / U)); > // access here must be U or real(), same with V } > > public ComplexNumber times(ComplexNumber Z) { > return new ComplexNumber(U * Z.U - V * Z.V, U * Z.V + V * Z.U); } } > > I think it accomplish to OOP rules (encapsulation), the > object itself manages its state interacting with its fields > and no other can change their properties, but I've seen many > samples that use getters methods to access values, I don't > know why...any clue ??? > > =============================================================> ============> To unsubscribe, send email to listserv@(protected) and > include in the body of the message "signoff J2EE-INTEREST". > For general help, send email to listserv@(protected) and > include in the body of the message "help". >
==========================================================================To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help".
|
|