Java Mailing List Archive

http://www.junlu.com/

Google
Google
Mailing List
Home
Forum Home
JBoss - Java Application Server
Tomcat - JSP/Servlet container
Struts - A MVC web framework
iText - An open source PDF Java Library
JDOM - JDOM XML Parser
JSP - A mailing list about Java Server Pages specification and reference
J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition
J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog
Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology
Struts & Hibernate
Subjects
JSP editor plugin for eclipse ?
org apache jasper JasperException: Unable to compile class for JSP
Tomcat: Connection reset by peer: socket write error
Cannot retrieve definition for form bean null
Struts Tiles Tutorial (free Struts training)
Where do I download Tomcat 4 0 6?
Data Access Object (DAO) pattern, example DAO 's
Where to download Tomcat v 4 1 24 from?
Tomcat 5 0 16 Requested resource not available
Subject: Servlet : Session invalidate
Oracle Connection Pooling in 3 2 2
Servlet action is currently unavailable
Tomcat/Struts Unicode Encoding/Decoding problems
Subject: Running a Simple JMS Example
Tomcat and webapplication specific java library path
Mapping in workers2 properties
org apache jasper JasperException
problem with html:text bean throwing exception
Cannot find message resources under key org apache struts action
   MESSAGE
Cannot find message resources under key org apache struts action MESSAGE
invalid direct reference problem with solution
Tool for jsp debug Try Sysdeo Eclipse Plugin
Tomcat 5 Cannot load JDBC driver class 'null ' SQL state: null
weblogic ejbc
java properties file
Jboss 3 2 3 Coyote Can 't re
Tomcat 5, Apache2 and mod jk2 integration problem
JBoss example problem new to J2EE
Value attribute of <html:checkbox
url string for connecting jboss to oracle
javax servlet ServletException: BeanUtils populate
5 0 18: Windows XP Pro vs Windows 2000
HTTP Status 404 The requested resource is not available
 
Subject: Var access question...

Subject: Var access question...

2006-11-01       - By Duc Vo

 Back
Reply:     1     2     3     4     5  

I don't fully agree with Tim though. The idea is only true when you have
attributes as primitive or simple object type. With complex object type
or expensive-to-create type, getters (private or not) are much better
for says lazy initialization or unit testing, also make your code much
cleaner.

Duc

-- --Original Message-- --
From: A mailing list for Java(tm) 2 Platform, Enterprise Edition
[mailto:J2EE-INTEREST@(protected)] On Behalf Of Tim Wood
Sent: Thursday, 2 November 2006 7:10 AM
To: J2EE-INTEREST@(protected)
Subject: Re: Var access question...

I'll probably get flak for this BUT... :)

Defining and using getters for private state just adds needless
indirection and makes the resulting Java even more function-call
intensive.  I still like the naming convention approach:
public class ComplexNumber {
       private double m_real;
       private double m_imaginary;
...
       public double magnitude() {
               return (Math.sqrt(Math.pow(m_real, 2)
                       + Math.pow(m_imaginary, 2)));
       }
...
}

This clearly distinguishes members from locals, and lets method code
work concisely with private data.  Tools like checkstyle can enforce the
convention.  I agree having member names look like local names can be
confusing.  In the event that the method body needs to be moved away
from the state it touches, accessors will be needed.  The naming
convention makes it easy to safely change the references to accessors,
if you don't have an IDE to do the refactoring for you.

HTH,
TW


At 10:43 AM 11/01/06, Karr, David wrote:
>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".

©2008 junlu.com - Jax Systems, LLC, U.S.A.