• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Please explain working of this code (Item 16 of Effective Java book Edition#1)

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code is from Item 16 - Prefer interfaces to abstract classes (Effective Java Ed#1) - Page# 86

I am kind of puzzled by looking at this code. I have several doubts and by looking at the sun docs, I could answer few of them. Please help for the rest:

1. Is the code in the return statement over ridding the get/set/size methods - YES (got the answer after I checked the API docs and it says that these methods must be overridden). But I do not see extends anywhere?
2. AbstractList is an abstract class as per the API docs, how can a new be done on it without extending? (Line 1)
3. When and where the values from int[] a, gets assigned to Integer List? Within the object of AbstractList, where are these values kept? (May be I am posting a non-sense question, as not sure what is the behavior)

Probably after the explanation, I would also get why this implementation is termed as Adapter patter in the para below the code.
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do a search on anonymous classes. That should get you quite far already.
 
Piyush Porwal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wonderful, looked deeper than my normal understanding of anonymous classes and found the answer for point 1 and 2. Thanks.
Some info is discussed here: http://forums-beta.sun.com/thread.jspa?messageID=715086

I am yet to find answer for point 3, any further pointers?
 
Rob Spoor
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The AbstractList subclass uses an implicit instance variable to the array.

You can really see this if you decompile the inner class file (IntArray$1.class), for instance using JAD (comments are mine):
 
Piyush Porwal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks much for your effort. Didn't know java does so much for you!!
Although extending anonymous classes this way seems natural and common, but not sure when should this way of implicit instance variable should be used. Let me read bit more on this and if I found any good link will post here.
 
Rob Spoor
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You already used the implicit variable, by using array "a' in your anonymous class.
 
The moustache of a titan! The ad of a flea:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic