• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about constructors when a object is called by the initialization

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks.
I'm new in Java and I've completed the Head First Java and now Im reading Head First Object Oriented Analysis and Design.
Turns out that I was understanding everything but there is some piece of code that I would like to review with you because its still not so intuitive.
When the following call is made:

What does the door in BarkRecognizer mean? That means that it will pass the object to the BackReconiser class, is that right?
But what does this does exactly?



I simply cannot understand what this.door = door means and why the constructor have this. Is this only to NOT use inherence?
What is the name of the technique? How can I study more of this to understand better?

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Torales wrote:I simply cannot understand what this.door = door means



There are two door variables in scope at the point of that statement: one instance variable, and the parameter.

this.door references the instance variable, the this. prefix mean to look for the variable in the current instance

door refers to the parameter

So this.door = door sets the instance variable to the parameter.

Consider if the parameter were named differently:

Same code, different names.

It's conventional to use the same name.

If the statement were written door = door it wouldn't work because both references would be to the parameter, assigning it to itself (which is pretty useless).
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is a standard idiom in constructors and setXXX methods. Remember that local variable hide fields, so in order to make the assignment apply to the field, you have to prepend this. to the variable name. Remember that = is pronounced “becomes equal to”, so you could pronounce
this.door = door;
as

The field door becomes equal to the local variable door.

For the purposes of this post, parameters are a sort of local variable.
 
Daniel Torales
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand the "this" and everything. But I'm under the impression that the code: is only to the Class BarkRecogniszer be able to call the methods of the class "DogDoor" that is passed by the line: Im right? If so, Im just under the impression that the same code might be used using inherence or other tricks.
I'm not sure if this is kind of a design pattern, or how can I study more about this pattern.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Torales wrote:same code might be used using inherence


No. Inheritance is used to model "is a" relationships -- not to create associations with other objects.

or other tricks.


"tricks" are never good. Never. Ever. Never try to think of "tricks" to get something done.

 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the BarkRecognizer is not a door, so it would not make sense for it to inherit from door. It does need to use the door though so it can open the door when it recognises a bark.

In general though inheritance should be used sparingly. It can be quite tempting to jump to inheritance to model all problems. There is a design pattern called Favour Composition over Inheritance.

http://www.artima.com/lejava/articles/designprinciples4.html

This link has Eric Gamma explaining why this is a good thing. The link may be a bit old, but what he's saying is still very relevant.

If you come from a different background (for example python) then inheritance might be used much more.
 
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:There are two door variables in scope at the point of that statement: one instance variable, and the parameter.


Is this is correct?
I have read somewhere java allows two variable to be named same until they both are in scope.


 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, of course it is correct. You cannot have two local variables with the same name, nor a local variable and a parameter with the same name, in the same scope.
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was asking is this is correct to say an instance variable and parameter are in scope,even though they have same name
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The local variable/parameter and the field are both in scope from line 10 to line 13, also lines 15 to 20.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic