Results 1 to 5 of 5
  1. #1
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default When to use this in java.

    hey guys, i just had a quick question. in this example:
    Java Code:
    public class Person {    
    //Private fields   
    private String firstName;   
    private String middleNames;  
     private String lastName;  
     private String address; 
      private String username;    
    //Constructor method   
    public Person(String firstName, String middleNames, String lastName, String address)   {     
    this.firstName = firstName;  
     this.middleNames = middleNames;  
     this.lastName = lastName;  
     this.address = address; 
       } 
    }
    what is the advantage of using this.firstName =firstName; instead of just having one variable be firstName and the other variable be fname? in other words, why use this instead of just having two different variables?
    Last edited by swedishfished; 10-14-2014 at 01:02 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: When to use this in java.

    Quote Originally Posted by swedishfished View Post
    what is the advantage of using this.firstName =firstName; instead of just having one variable be firstName and the other variable be fname? in other words, why use this instead of just having two different variables?
    Well, I personally don't like cluttering up the symbol table (even if the variables are local to the constructor). But
    you can do it like this.

    Java Code:
    class Foo {
        int val;
        class Foo(int v) {
          val = v;
        }
    }
    And of course there are other uses for this too.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default Re: When to use this in java.

    ok thanks jim, cuz that was what i've been doing.
    i cleaned up the code, it was a bit messy.

    what other uses are there for this? are any of these uses unique to this (can't be done without using this)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: When to use this in java.

    Quote Originally Posted by swedishfished View Post
    what other uses are there for this? are any of these uses unique to this (can't be done without using this)
    Calling one constructor from another.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

Posting Permissions

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