• 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

What does it mean to parameterize a class definition?

 
Ranch Hand
Posts: 353
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source of code snippet:  SCJP Exam 310-065 By Kathy Sierra and Bert Bates, page 626



The author says the class definition above "is a ridiculous use of generics". The definition may be ridiculous, but it does exist for a reason.  So, I replaced the generic type in the class definition as shown below.  


The class compiles without error or warning, but what exactly does the <ArrayList> confer on the class?
Aside:  I will appreciate it if someone can explain how to format the subject/title.  For example, how to make it bold.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't try lots of formatting; it doesn't make for easy reading. I don't think you can change the attributes of title code.

You appear to have found the wrong forum for your question; this forum is for disccussing this website. I shall move you somewhere better. Thank you for providing full details of the source of that quote

Maybe it would be easier to understand that you have a trivial case of generics there. That looks like a simple data transfer class; you create an object with an instance of a particular type and when you use it later, you will get an object of the same type back. Let's look at an example:-The fact that the toLowerCase() call compiles shows that the compiler is correctly interpreting your generics. We know that String is one of a few classes to have an instance toLowerCase() method.

What you have overlooked is that the format of the formal type parameter is not fixed. Should you use a different formal type parameter, that will be interpreted as simply a formal type parameter by the JVM. If you change all the Ts to Es in that code, it will make no difference to how it is used nor to its functionality. Let us try doing some compiling:-Now, change all the Ts to Es and you get no difference in functionality. Change all the Es to ArrayList and all you are doing is using an incorrect form for the formal type parameter. If you
try the same code with the T changed to ArrayList, you get exactly the same result. Try it. Even though the bytecode might look different:-The word ArrayList has nothing to do with the several classes of that name; it is again a placeholder. All you are doing is spelling the T as A‑r‑r‑a‑y‑L‑i‑s‑t. Try running the lines 15‑16 from my earlier code block, and you will see the anInstance field behaves as a String, not some sort of List.

I think you will find out how to make a container that accepts Lists only if you go through the Java™ Tutorials.
 
Biniman Idugboe
Ranch Hand
Posts: 353
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Mr. Campbell.  I have taken special note of  

format of the formal type parameter




Please, bear with me!  Is the empty < > implying the parameter type from the left hand side?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Biniman Idugboe wrote:Thanks a lot Mr. Campbell.  . . .

That's a pleasure

Is the empty < > implying the parameter type from the left hand side?

Yes. See the Java™ Tutorials section I showed you earlier and find out about what they call the diamond operator.

Foo<Bar> foo = new Foo(); without <> or <Bar> is called using a raw type. If you try that, you will find it is probably possible to add anything to the instance, in which case, goodbye type‑safety.
You must have changed the class, because the original code you showed had a one‑argument constructor. Try changing line 15 of my first code block to this and see what happens, both at compile time and at runtime:-Obviously, with the <> or <String>, you could never pass 123, which is boxed to an Integer object, to that constructor.
 
Biniman Idugboe
Ranch Hand
Posts: 353
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarifications.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a pleasure
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic