• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between Attribute and Instance Varaible

 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic