Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doub about constructors  RSS feed

 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm here today for a doubt i'm having with constructors, this is the code:



The output of the code is:

type 1 duck
type 5 duck
type 3 duck
type 2 duck
type 5 duck
type 2 duck
type 4 duck


I agree with constructors number 1, 2 and 3. But what about the others?

In d[1] i've density and weight, so i add floatability (equals to density), and maxspeed.
In d[4] i specify the float value of density, and i know the airspeed, so i add only the maxspeed.
In d[6] i know airspeed and density so i add pounds and flotability?
->right?


And in Duck class i've some instance variables, and in TestDuck i've others, why this? The ones in TestDuck are simply more specific?

Need help!
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Addeo wrote:

I agree with constructors number 1, 2 and 3. But what about the others?

In d[1] i've density and weight, so i add floatability (equals to density), and maxspeed.
In d[4] i specify the float value of density, and i know the airspeed, so i add only the maxspeed.
In d[6] i know airspeed and density so i add pounds and flotability?
->right?


What is it that you disagree with about the constructor calls you mention?

In d[1] you call the Duck(float, int) constructor because density is a float and weight is an int.
In d[4] you also call the Duck(float, int) because your first parameter is a float literal, and airspeed is an int.
In d[6] you call the Duck(int, float) constructor because airspeed is an int and density is a float.

What result where you expecting when you called these constructors?

Ivan Addeo wrote:
And in Duck class i've some instance variables, and in TestDuck i've others, why this? The ones in TestDuck are simply more specific?

Need help!


There are no instance fields in TestDuck, but there are some local variables in the main(String[] args) method. These are completely unrelated to any of the instance fields in Duck, and no Duck instance has any idea that these local variables exist. No Duck instance even has any knowledge that the TestDuck class itself exists.

But you wrote the code didn't you, so surely you know why you created those variables? If you didn't write the code, where is it from?
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use floats, not even for floatability, unless some peculiarity of another API forces you. If you want floating‑point numbers use doubles.

This may be a better way to initialise that array:-
Duck[] d ={new Duck(), new Duck(density, weight), new Duck(name, feathers),
    new Duck(canFly), new Duck(3.3F, airspeed), new Duck(false), new Duck(airspeed, density)};


The initialiser means you are not risking leaving the array with a lot of nulls as its elements.
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:
Ivan Addeo wrote:

I agree with constructors number 1, 2 and 3. But what about the others?

In d[1] i've density and weight, so i add floatability (equals to density), and maxspeed.
In d[4] i specify the float value of density, and i know the airspeed, so i add only the maxspeed.
In d[6] i know airspeed and density so i add pounds and flotability?
->right?


What is it that you disagree with about the constructor calls you mention?

In d[1] you call the Duck(float, int) constructor because density is a float and weight is an int.
In d[4] you also call the Duck(float, int) because your first parameter is a float literal, and airspeed is an int.
In d[6] you call the Duck(int, float) constructor because airspeed is an int and density is a float.

What result where you expecting when you called these constructors?

Ivan Addeo wrote:
And in Duck class i've some instance variables, and in TestDuck i've others, why this? The ones in TestDuck are simply more specific?

Need help!


There are no instance fields in TestDuck, but there are some local variables in the main(String[] args) method. These are completely unrelated to any of the instance fields in Duck, and no Duck instance has any idea that these local variables exist. No Duck instance even has any knowledge that the TestDuck class itself exists.

But you wrote the code didn't you, so surely you know why you created those variables? If you didn't write the code, where is it from?


Thank you all for replies!

In d[2] i've name and feathers, in the constructor i've name=n; and feathers=f; - That's ok for me

But why in d[1] for example, if i've the int weight, there is no track of weight in the constructor? i've density=floatability, and maxSpeed=max.. what about weight?

The code is an exercise for Head First Java at pag. 248.

name in d[0] is Generic, name in d[2] is Donald, got it.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The name of the variable you pass to the constructor has no bearing on which constructor is called. If you pass a float and an int to the constructor then the compiler will look for the method that can take a float and an int.

The names you give to variables are for your own convenience when reading the code. You could give all of your variables completely nonsensical names and it would still function in exactly the same way.
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About names, yes i know, i was trying to figure out how to "connect" those instance variables and those local variables. When i use this name instead of this? etc.

For the constructors, i'm thinking on it right now.

Can you explain your last concept again? d[1] need density and weitght, but the constructor has no weight. Yes i need an int but it is called max and is equalised to maxSpeed.

If i try to print weight value, i've to add weight variable in class Duck, and the value is 0, of course. Now i don't understand why if i say public int weight=8; in TestDuck class, instead of int weight=8;, i've an illegal start of expression. I'm playing with weight value..

Of course the constructor want a float and an int, but when values are passed, what about maxSpeed? It has same value of weight?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Addeo wrote:About names, yes i know, i was trying to figure out how to "connect" those instance variables and those local variables. When i use this name instead of this? etc.

I don't quite follow that sentence, but it would appear that you have a basic misunderstanding of what Java names do.

Fields with the same name aren't magically "connected" to each other - and if they were you'd be in a lot of trouble, because you'd soon run out unique ones to use.

Can you explain your last concept again? d[1] need density and weitght, but the constructor has no weight. Yes i need an int but it is called max and is equalised to maxSpeed.

That's not correct.

You've supplied density and weight - both of which are fields in TestDuck.main() - to the new() command for d[1].
That doesn't mean that you necessarily have a constructor that takes those values though - in fact, as you've already observed, you don't.

What you DO have however, is a constructor that takes a float and an int - the 5th one, so that's the one that the compiler assumes you want.

And it does that because it matches types - and ONLY types - when it's looking for a constructor; not names. As Mike says, names are purely for your convenience, so you can understand your code more easily; and they don't cross class (or even method) boundaries.

HIH

Winston
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not exactly, i can't express exactly in english. I'm saying that when i want the name of the duck i've different names, "Generic" and "Donald". It's Generic when i run the program with the empty constructor, and Donald when i run the constructor that use that name.

About constructor, so when in the 5th constructor i say maxSpeed=max; this code is absolutely not related with the int max argument of the constructor.. i understand this concept, but is a little confusing.. if i wrote this constructor i would have used a different name to avoid confusion.. same thing with the 4th constructor..

 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here you have declared a constructor for the object Duck. It will have the parameters density (a float) and max (an int). These names are significant inside the constructor only.

You are now using the parameters to set the instance variables. The names are significant.

Now you are calling the constructor. The variable names "density" and "weight" don't have any bearing on which constructor is called. But the types do. You have a float and an int so the compiler looks for a constructor with a float and an int in that order and it finds constructor "five". It still sets the floatability to the first parameter and the maxSpeed to the second, regardless of the names used when it was called.
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So where is the utility of calling it weight?

This suits better in my opinion:

public Duck(float density, int weight)

Probably i'm saying always same thig but i think i'll understand when i'll use my personal constructor and not the book one :P
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Addeo wrote:Not exactly, i can't express exactly in english. I'm saying that when i want the name of the duck i've different names, "Generic" and "Donald". It's Generic when i run the program with the empty constructor, and Donald when i run the constructor that use that name.

And that's precisely what you can't do.
All constructors have the same name - in your case: 'Duck' - so the only thing that distinguishes them is the number and types of their arguments.

And if you don't believe me, try adding a 6th constructor like this:and see what happens.

Specifically: you will get a compiler error because you now have two constructors with the same signature.

About constructor, so when in the 5th constructor i say maxSpeed=max; this code is absolutely not related with the int max argument of the constructor.. i understand this concept, but is a little confusing.. if i wrote this constructor i would have used a different name to avoid confusion.. same thing with the 4th constructor..

I have to admit, it isn't the most clearly written example I've ever seen; but sometimes books do that to make sure that you understand the concept.

Just remember this: The compiler is ONLY interested with the types of things you pass to a constructor (or method); it doesn't care what they're named.

Winston
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, i'll keep all this informations in mind during my learning, thank you all
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!