Results 1 to 10 of 10
Like Tree3Likes
  • 2 Post By KevinWorkman
  • 1 Post By JosAH

Thread: Why must constructor arguments be stored manually?

  1. #1
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default 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.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default 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!

  3. #3
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default 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?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default 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.
    Zigster and Fubarable like this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why must constructor arguments be stored manually?

    Quote Originally Posted by Daimoth View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default 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.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why must constructor arguments be stored manually?

    Quote Originally Posted by Daimoth View Post
    So if two variable names aren't stepping on each others' toes, I don't need to use "this" at all?
    Indeed; the name of a parameter has nothing to do with the name of a member variable; if there's a clash a simple this.member solves the problem.

    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Why must constructor arguments be stored manually?

    And they are stored because arguments are no longer accessible after the constructor has run?

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default Re: Why must constructor arguments be stored manually?

    Quote Originally Posted by Daimoth View Post
    And they are stored because arguments are no longer accessible after the constructor has run?
    If they are stored, that's probably the reason for storing them.

    You might want to throw together a few example programs to test out what we're talking about.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Why must constructor arguments be stored manually?

    Thanks guys, you were both a lot of help.

Similar Threads

  1. Replies: 8
    Last Post: 09-23-2010, 12:07 AM
  2. Creating a constructor with arrays and arguments - Part 2
    By fullmetaljacket in forum New To Java
    Replies: 15
    Last Post: 07-05-2009, 01:38 PM
  3. Creating a constructor with arrays and arguments
    By fullmetaljacket in forum New To Java
    Replies: 38
    Last Post: 07-03-2009, 06:09 AM
  4. Manually adding a component
    By djc in forum NetBeans
    Replies: 3
    Last Post: 04-01-2009, 11:51 AM
  5. Handle exception manually
    By John_28 in forum New To Java
    Replies: 2
    Last Post: 06-05-2008, 11:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •