Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why wrapper simple object making not working?

 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the Javadoc have to say about it?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why they made it immutable...if i do like this...

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

 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's changing the reference, not changing the object.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the other constructor?
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Default Constructor:
http://en.wikipedia.org/wiki/Default_constructor

Hunter
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote: . . . It does say that now and has for the last 20 minutes.
I like it Thank you
 
Paul Clapham
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, thank you to whoever ought to be thanked
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...........
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shanky sohar wrote:i think default constructor is being provided by compiler...........
Yes, it is.
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from wrapper class object to- Wikipedia- from Wikipedia to default constructor....Are we going to the right path guys?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so can we have the final conclusion....
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christophe...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic