• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can I create a constructor with parameters and methods in the same class?  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The one problem in my book was to create a constructor for different shirt features, which I did and ran successfully. Out of curiosity, I also added other methods to see if it would run if the parameters were different from the constructor. It keeps giving me a constructor error. So, my question is, am I able to create a class that uses a constructor with parameters and other methods without errors? I'm guessing there's no reason to since it would be wasted space since the constructor could do it but was just curious if it's possible.

Is everything from the constructor down (in the class) and Shirt.oneShirt (in the main) just a waste of time?

Here's my example:






 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this line is causing the problem?

You don't have a no-argument constructor so the compiler gives you an error.
Yes, it is perfectly legal to make getters and setters like you did.

Is it a waste of time? I don't know. It depends of what you need.
If you need Shirt to be mutable, it is not waste of time.
If you need Shirt to be immutable, not only is existence of setters a waste of space but also it ruins the idea of being immutable.
It is nice to have getters if you need this data form your class.
Getters in your class are not waste of space (because you need field values in TestShirt class).
But you should mark the fields private.

Thank you for using code tags but it would be much nicer if you avoided putting lines so long.
There is a link: DontWriteLongLines
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I edited above so it wasn't so long. Sorry about that (just learned I can hit enter at the "+") and will keep it short in the future.

I edited it to include what's below and everything works now. Thanks so much!



 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not a default constructor, but a no‑arguments constructor. I suggest you delete it, unless a 0‑size shirt is a permissible object.
 
Abhishek KumarSoni
Ranch Hand
Posts: 61
Chrome Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Following line is giving you constructor error

In your code there is no such constructor which takes no parameter as argument.Since you have declared your own constructor so compiler is not going to provide you a default constructor.If you still want you have to mention it in your code manually as follow

Now since you have a no arg constructor your code runs.

Gob Bluth wrote:
Is everything from the constructor down (in the class) and Shirt.oneShirt (in the main) just a waste of time?


We can not say it a waste of memory.It all depends on usage of your program. Let us take your code.
In your code you will initialize your Shirt variable as follow

Now after initializing this you want to change your Shirt's collarSize then setCollarSize will be helpful to set collarSize for this particular instance of Shirt.

Although in your code you can change it directly

but this is right untill you are in same package outside the package you have to use setters since your instance variable are of default access.
This was all about set method Second let us take getter of this code.
Getter in a class can never be a waste of memory.Because getters provide you encapsulation.In your code instance variable are default so their's value can be accessed directly in same package as follow

But If you want to access your collarSize outside the package than you cant access it as follow

It will report an error because collarSize is of default access and is not accessible outside the package.
Then getter will retrieve this value for you as follow

One case if your declare all of your instance variable as public then we can say getters are just wasted of time.but in that case your code will not remain encapsulate.So declare all the instance variable as private and provide getters for their access outside class.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By deleting the no‑arguments constructor (which I am sure was the right thing to do), you are telling the compiler, “Every shirt must have those three sizes provided.”

Whenever you attempt to create a Shirt object, you must say new Shirt(15, 32, 1) or similar.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should provide the Shirt class with a toString method, then you can say
Shirt s = new Shirt(15, 31, 2);
System.out.println(s);

… and get a sensible output.

Don't replace the no‑arguments constructor. I have a little rule of thumb that every class must have a written constructor, but you should write as few constructors as you can. Also every constructor must initialise every field (directly or indirectly) to a sensible value.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of the advice.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!