This is a common confusion because most of the time, especially in example
code, there is no discernable distinction. However, there's no necessary
correlation between a JavaBean's property(ies) and a JavaBean's instance
variable(s), other than that there must be _some_ instance variable(s)
behind the scenes which are accessed via the Bean's setter and getter
methods.
Properties go like this:
public void setPropertyName(TYPE arg)
{
foo = arg;
}
public TYPE getPropertyName()
{
return foo; //where foo is a variable of the specified TYPE
}
Fine. Simple case. But note that the instance variable foo doesn't have to
have the same name as the property name. The property name is specified in
the method names and nowhere else. There's an intentional divorce between
property names and instance variables because the whole idea is to
encapsulate the private instance variables and any attendant logic in the
Bean, viewing the Bean as an "object with properties," not as a "Java class
with instance variables and methods." It's all in the way of OOP thinking.
Here's an example that from a JSP book I'm working on with Ben Forta and
some other guys. I attempt to make clear the private variable/public method
property distinction by using different names for them, and using two
distinct read-only and write-only methods (the NewSession and Hits
properties) to access the same instance variable, hitCount. Hope this
helps. Any comments welcome, as this example is going in the book. :-)
The accompanying (simplified for this example) JSP is below the Bean:
import java.util.Date;
import java.text.DateFormat;
import java.io.Serializable;
public class HitCountBean implements Serializable
{
private boolean b;
private int hitCount;
private String serverStart;
private Date date = new Date();
public String getStartTime()
{
return serverStart;
}
public int getHits()
{
return hitCount;
}
public void setNewSession(boolean b)
{
b = b;
if (b)
{ hitCount++; }
}
public HitCountBean()
{
hitCount = 0;
serverStart =
DateFormat.getDateInstance(DateFormat.LONG).format(date);
}
}
****************************
<jsp:useBean id="counter" scope="application" class="HitCountBean" />
<jsp:setProperty name="counter" property="newSession"
value="<%= session.isNew() %>" />
<html>
<head><title>Hit Counter</title>
<style type="text/css">
<!--
body { background-color: #ffffff;
font-family: sans-serif;
text-align: left; }
h1 { text-align: center;
color: #cc0000;
text-decoration : underline; }
-->
</style>
</head>
<body>
<h1>Hit Counter</h1>
<p>This page has had <jsp:getProperty name="counter" property="hits" />
user sessions since the server started on <jsp:getProperty name="counter"
property="startTime" />
</p>
</body>
</html>
Scott Stirling
-----Original Message-----
Sent: Thursday, May 25, 2000 11:34 PM
Subject: instance variables vs. properties of javabeans
could anyone please tell me the differences between instance variables
vs. properties of javabeans?
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets