Results 1 to 10 of 10
- 05-29-2012, 07:17 PM #1
Why must constructor arguments be stored manually?
Say some simple class takes three arguments, cat, dog, and mouse. I often see the "this" keyword in the constructor to store them all as variables.
I understand that it's necessary to store them for use inside the class, but why is it necessary? Why can't they be used immediately?
This is something I do, but don't understand.Last edited by Daimoth; 05-29-2012 at 07:40 PM.
- 05-29-2012, 07:37 PM #2
Re: Why must constructor arguments be stored manually?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-29-2012, 07:41 PM #3
Re: Why must constructor arguments be stored manually?
Thank you, but I understand the basics of that keyword. I understand how to store arguments as variables, but what I don't understand is why it's necessary to store them at all. Why aren't they available for immediate use? Can arguments not be treated like variables?
- 05-29-2012, 08:02 PM #4
Re: Why must constructor arguments be stored manually?
You're asking a couple different questions which don't immediately have to do with one another.
You use the this keyword to refer directly to a class variable when a local variable has the same name and is therefore hiding the class variable. If the class variable has a different name than the local variable, you don't have to use the this keyword. That's in the tutorial I linked for you.
The other thing you're asking about is the scope of a variable. Class variables have scope inside the class. Arguments and variables declared inside a method have scope only inside a method (so you can't use it inside a different method). Variables declared inside an if statement or loop only have scope inside that block. You set a variable with a wider scope equal to a value with a smaller scope if you want to use that value outside of the scope in which it was declared.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-29-2012, 08:20 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Why must constructor arguments be stored manually?
No, arguments (parameters) are gone when the method or constructor has gone too; there is nothing magical about constructors and even more: a constructor can do what it wants with the parameters, i.e.
it doesn't have to store them verbatim in its class variables.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-29-2012, 08:29 PM #6
Re: Why must constructor arguments be stored manually?
So if two variable names aren't stepping on each others' toes, I don't need to use "this" at all?
Last edited by Daimoth; 05-29-2012 at 08:34 PM.
- 05-29-2012, 08:32 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Why must constructor arguments be stored manually?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-29-2012, 08:34 PM #8
Re: Why must constructor arguments be stored manually?
And they are stored because arguments are no longer accessible after the constructor has run?
- 05-29-2012, 08:35 PM #9
Re: Why must constructor arguments be stored manually?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-29-2012, 08:51 PM #10
Similar Threads
-
Sorting objects with 2 strings arguments and 2 int arguments
By tirwit in forum New To JavaReplies: 8Last Post: 09-23-2010, 12:07 AM -
Creating a constructor with arrays and arguments - Part 2
By fullmetaljacket in forum New To JavaReplies: 15Last Post: 07-05-2009, 01:38 PM -
Creating a constructor with arrays and arguments
By fullmetaljacket in forum New To JavaReplies: 38Last Post: 07-03-2009, 06:09 AM -
Manually adding a component
By djc in forum NetBeansReplies: 3Last Post: 04-01-2009, 11:51 AM -
Handle exception manually
By John_28 in forum New To JavaReplies: 2Last Post: 06-05-2008, 11:26 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks