The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes difference between Attribute and Instance Varaible Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "difference between Attribute and Instance Varaible" Watch "difference between Attribute and Instance Varaible" New topic
Author

difference between Attribute and Instance Varaible

Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
What is the difference between a attribute(request.setAttribute()) and Instance variable ?

I mean to ask, by adding an attribute to request object we are adding a new instance variable to the request object(isn't ??) so isn't it similar to having an addition instance variable to that particular object? If so what is the difference between Instance variable and an attribute

I tried to answer my question to myself as ,"we are only adding to thar particular request object , not for every request object."
Royston Monteiro
Ranch Hand

Joined: Apr 25, 2008
Posts: 35
Attributes are different from instance variables in their scope. Of course, they can be in page, request, session or application scope and are meant to pass the objects they are assigned to among classes that have access to those scopes.
Instance variables have scope only within a particular class/object and are always assosiated with a particular class.

Attributes are meant to be used as messages on a notice board so that classes the have access to a particular request, page, session or application can usethem.



Cheers,
Roy.
SCJP 1.5, SCWCD 5.
"Perseverance is the hard work you do after you get tired of doing the hard work you already did"
Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
Royston Monteiro wrote:Attributes are meant to be used as messages on a notice board so that classes the have access to a particular request, page, session or application can usethem.

But even instance variable(object) can pass information to classes which has access to that object like...
class A{}

class B{
new A();
}

Now the object B has access to the class A and that HAS-A relation can pass the information to the Class A. Isn't it???


Keith Flo
Ranch Hand

Joined: Nov 29, 2005
Posts: 126

The explanation above is a good one ... IMO ...

however, I like to think of the 'contexts' and 'scopes' as like areas where certain kinds of objects reside ( ... live out their lives). For example: a web app lives in 'application' scope. requests in request scope, etc. Adding an attribute, to me, adds an object into their 'house' that objects in your app can (when they have a reference to the 'context') interact with ... view, modify etc.

Thats how I think of it ..

By the way ... the bulletin board analogy is used in the K&B book on page 185 to describe attributes.


kf
SCJP 5.0 (preparing for SCWCD)
 
 
subject: difference between Attribute and Instance Varaible
 
Threads others viewed
object Vs instance
object Vs instance
Related to Object
Instance varaible in JSP scriplet
instance/reference
MyEclipse, The Clear Choice