File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes why wrapper simple object making not working? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why wrapper simple object making not working?" Watch "why wrapper simple object making not working?" New topic
Author

why wrapper simple object making not working?

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

if i do this



gives me error ..... why it's so ?? what happens to it's default constructor...? and if it doesn't have any default constructor why it's so?


No Kaustubh No Fun, Know Kaustubh Know Fun..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

What does the Javadoc have to say about it?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

There's no default constructors. Check the API. You should know that if you overload a class constructor by passing parameters to it, the default constructor will not be created. Moreover, wrappers are immutable. If you don't set a value when the wrapper is instanciated, when are you going to do it ?


[My Blog]
All roads lead to JavaRanch
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Wrapper class objects are immutable, so the value should be specified in the initialization time! So they make class having the constructor to behave as immutable objects!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

why they made it immutable...if i do like this...

Integer i = new Integer();
i=25;

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

That's changing the reference, not changing the object.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Kaustubh G Sharma wrote:why they made it immutable...if i do like this...

Integer i = new Integer();
i=25;


It means, you've changed the variable i to refer another Integer object. Immutable object means, changing the object state.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Christophe Verré wrote:If you don't set a value when the wrapper is instanciated, when are you going to do it ?

In some of the Java classes[like ArrayList] ,argument constructor is called from default constructor . so if there could be some thing like below, it wont be good?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4339
    
    7

The designers could have done that, but it wouldn't really add anything. And since they chose not to do that then there is no default constructor.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Seetharaman Venkatasamy wrote:
Christophe Verré wrote:If you don't set a value when the wrapper is instanciated, when are you going to do it ?

In some of the Java classes[like ArrayList] ,argument constructor is called from default constructor . so if there could be some thing like below, it wont be good?


Please excuse, if I'm wrong!

With this scenario, what can we do, other than making a immutable Integer object with 0 value?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Use the other constructor?
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Christophe : The code shown in the OP is attempting to call the "no-arg" constructor.
"Default constructor" is a compiler concept and using the term here is confusing and
misleading. Because it's immutable, Integer does not have a no-arg constructor.

Jim ... ...


BEE MBA PMP SCJP-6
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

if there's no default constructor for wrapper then it make all concept of dafalut means as all classes extends defaultly object class then there's must an default constructor for wrapper also..?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Kaustubh G Sharma wrote:if there's no default constructor for wrapper then it make all concept of dafalut means as all classes extends defaultly object class then there's must an default constructor for wrapper also..?


No need of default constructor for every classes. If you own constructor won't break the object creation chain, then it's OK.

Did I understand your question wrongly?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Kaustubh G Sharma wrote:if there's no default constructor for wrapper then it make all concept of dafalut means as all classes extends defaultly object class then there's must an default constructor for wrapper also..?

Just because class Object has a default constructor doesn't mean that any class that extends Object needs to have a default constructor.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Jesper, Abimaran, Kaustubh : What is this "default constructor" that all
of you speak about? Does it have any relationship to the "no-argument"
constructor? How do I program and use a "default constructor"?

Jim ... ...
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Default Constructor:
http://en.wikipedia.org/wiki/Default_constructor

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37923
    
  22
That Wikipedia article is incomplete; the default constructor is (as they say) automatically generated, and takes no arguments (which they suggest by calling it nullary) but they forget to say it does nothing.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Campbell Ritchie wrote:That Wikipedia article is incomplete... they forget to say it does nothing.

It does say that now and has for the last 20 minutes.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37923
    
  22
Paul Clapham wrote: . . . It does say that now and has for the last 20 minutes.
I like it Thank you
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Campbell Ritchie wrote:
Paul Clapham wrote: . . . It does say that now and has for the last 20 minutes.
I like it Thank you

The original edit which added that sentence was made anonymously and not by me. I just edited it again to fix a very minor typo. So our thanks go to one of Wikipedia's many anonymous editors (who may well have got the idea from here).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37923
    
  22
Well, thank you to whoever ought to be thanked
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Campbell Ritchie wrote:That Wikipedia article is incomplete; the default constructor is (as they say) automatically generated, and takes no arguments (which they suggest by calling it nullary) but they forget to say it does nothing.


i think default constructor is being provided by compiler...........


SCJP6.0,My blog Ranchers from Delhi
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37923
    
  22
shanky sohar wrote:i think default constructor is being provided by compiler...........
Yes, it is.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

from wrapper class object to- Wikipedia- from Wikipedia to default constructor....Are we going to the right path guys?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37923
    
  22
Kaustubh G Sharma wrote:from wrapper class object to- Wikipedia- from Wikipedia to default constructor....Are we going to the right path guys?
Since "default constructor" was mentioned in the second reply by Christophe Verré, I think this discussion is proceeding naturally along a correct path.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

so can we have the final conclusion....
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Kaustubh G Sharma wrote:so can we have the final conclusion....

1. The API tells you that there are no constructors without parameters
2. The API tells you that there are two constructors, one having an int parameter, one having a String parameter
3. 1. and 2. imply that there won't be any default constructor (constructor without parameter)
4. new Integer() is not possible because of 3.
5. new Integer() would not make sense, because Integer is immutable, so you could not set its value afterwards
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

Thanks Christophe...
 
Don't get me started about those stupid light bulbs.
 
subject: why wrapper simple object making not working?
 
Similar Threads
Java Bean and Constructor
Use of constructor in abstract class?
overriding/loading
quick class question
Jtips Quiz4 No.1: About default constructor