It's not a secret anymore!
The moose likes Java in General and the fly likes class wrappers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "class wrappers" Watch "class wrappers" New topic

class wrappers

Cristian Negresco
Ranch Hand

Joined: Sep 15, 2001
Posts: 182
Could someone explain what the class wrappers are, when are they used, when not?
Does somebody know some usefull links?
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Well, there are several kinds of wrappers.
The most common are primitive wrappers.
If you have a method or Collection that only accepts Objects then you can't feed in a primitive value. One way around this is to "wrap" the primitive value in an Object that just houses the value of the primitive.
class Integer just holds one value. That is the value of an integer. Then it has a bunch of methods to manipulate that integer so that it can be "handled" as an object.
Integer myInt = new Integer(3);

"JavaRanch, where the deer and the Certified play" - David O'Meara
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
wrapper class
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
A wrapper class is an implementation of the Decorator Pattern. To learn more about what the Decorator Pattern is and why it's useful, take a look at the Structural Patterns section of James W. Cooper's The Design Patterns Java Companion.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
...and I just ran across an article at JavaWorld that does an ok job of introducing the concept of patterns (including the decorator pattern): "Amaze your developer friends with design patterns"...
I agree. Here's the link:
subject: class wrappers
jQuery in Action, 3rd edition