• Post Reply Bookmark Topic Watch Topic
  • New Topic

Some no argument constructors do not have any meaning and cannot exist  RSS feed

 
Saishradha Mohanty
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am a beginner in JAVA, it's been a month since I started doing JAVA. I am following a book for that.
I came across a doubt when I was reading through it. It says that some constructors without any argument have no meaning and cannot exist, like say I create a color class, then the constructor for any object of that class has to have some argument.
Why is that so?
Is there any pre existing class for color in JAVA?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
constructors without any argument have no meaning and cannot exist

That is not true.  Constructors arg not required to have arguments.
Class fields can have default values.
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saishradha Mohanty wrote:It says

Please tell the book and the page number.

Also please tell *exactly* what book says without your interpretation of rephrasing. It happens quite often that people misread/misinterpret what authors trying to say.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that indeed what the book is saying (see previous post), sounds like the trash is a good place for that book.

 
Saishradha Mohanty
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q.: Earlier you said that It's good to have a no-argu- ment constructor so that if people call the no-arg con- structor, we can supply default values for the "mlsslng arguments. But aren't there times when It's Impossible to come up with defaults? Are there times when you should not have a no-arg constructor In your class?
.A..: You're right. There are times when a no-arg construc- tor doesn't make sense.You'll see this in the Java API-some classes don't have a no-arg constructor. The Color class, for example, represents a...color. Color objects are used to, for example, set or change the color of a screen font or GUI button.When you make aColor Instance,that instance Is
of a particular color (you know, Death-by-Chocolate Brown, Blue-Screen-of-Death Blue,Scandalous Red,etc.),If you
make a Color object you must specify the color In some way.
Color c ; new Color(3,45,200);
(We're using three Ints for RGB values here.) Otherwise, what would you get? The Java API programmers could have de- cided that if you call a no-arg Color constructor you'll get a lovely shade of mauve. But good taste prevailed.
If you try to make a Color without supplying an argument:
Color c ; new Color();
The compiler freaks out because It can't find a matching no- arg constructor in the Color class.

Can you please explain what is happening here, acc. to this color class can't have a constructor without any argument.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is very different than saying a no-arg (nullary) constructor never makes sense.

In this case, the author(s) of the Color class decided that there were no defaults that made sense, so they did not provide a no-args constructor.

It could just have well have been decided that "black" (0,0,0) would have been a decent default. But they didn't.

That's all that is being said.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The decision on whether to have a no-argument constructor has nothing to do with language syntax but rather with design, semantics, and logic. Semantics and logic are both about "making sense."

Related to this is POLA or the Principle of Least Astonishment. If you provide a no-argument constructor, your object's fields should have sensible defaults that most people would expect. Take a Date object, for example. When instantiated with its no-argument constructor, the default will be to assign it a value of the current system date. That's what the class designers deemed as the default that makes the most sense. On the other hand, they didn't provide a no-argument constructor for the Integer wrapper class, even though a primitive int variable would be 0 by default. I don't know the exact reasoning behind that design choice but that's what it ended up being.

Edit: I would guess that the decision had something to do with the immutability of Integer objects.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!