aspose file tools*
The moose likes Beginning Java and the fly likes Wrapper Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wrapper Class " Watch "Wrapper Class " New topic
Author

Wrapper Class

Will Carpenter
Greenhorn

Joined: Mar 17, 2004
Posts: 26
I'm trying to pass a short to a method and get the short back, modified by the method. I know that won't work (call-by-value vs call-by-reference), so,
I know I need to somehow put my short into the Short Wrapper class. How?
Given:
short ansr = 1;
getTask(ansr);
switch(ansr)
{ case 1: .....etc
It's not really important, but getTask() is just a method that prompts
the user for what task they want to perform.
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Why not:


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Will Carpenter
Greenhorn

Joined: Mar 17, 2004
Posts: 26
Thanks Nathaniel! That worked.
I guess all of those initials after your name aren't just a recipe for alphabet soup!
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Hi Will,
Don't even think about using a wrapper to return a value by reference! The wrapper classes are great for conversions and Objectising values, but they don't have setter methods. That means that even though the number is passed in by reference, you cannot change it. If you point it at a new wrapper, then that wrapper's location will not be the same as that of the caller's wrapper. The new wrapper will be lost on exit from the called method.
Nathanial's answer is perfect for your situation but if you want to get values back through the argument list then you would need to write your own wrapper class where you can set and modify values. But then you would think about OOP and maybe that wrapper class would also contain other things to do with getting and validating the value???
Just something to think about.
Ed


The nice thing about Standards is that there are so many to choose from!
 
 
subject: Wrapper Class