Results 1 to 6 of 6
  1. #1
    mo_mughrabi is offline Member
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Default "this" function in Java

    Hi,

    am new to java and am learning through some books. there is one function here I couldn't really understand please see the class below

    Java Code:
    public class ThisTest {
    	public static void main(String args[]) {
    		SimpleTime time = new SimpleTime(15, 30, 19);
    		System.out.println(time.buildingString());	
    	}
    
    
    
    }
    
    class SimpleTime {
    
    	private int hour;
    	private int minute;
    	private int seconds;
    
    	public SimpleTime(int hour, int minute, int seconds) {
    		this.hour = hour;
    		this.minute = minute;
    		this.seconds = seconds;
    	}
    
    }
    my question is, what is "this.hour" i mean, what is "this" used for?

  2. #2
    JT4NK3D's Avatar
    JT4NK3D is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    my question is, what is "this.hour" i mean, what is "this" used for?
    It means the instance that is being created, in your case. In your head replace the "this" with "time", your SimpleTime instance name. Normally you wouldn't need to use 'this', except your constructor has 3 arguments that are called the same thing, so the part on the right of the = sign does not have 'this' because it's talking about the arguments, not the fields. This is called "shadowing", when arguments shadow fields. Normally, you wouldn't need 'this', except if it's shadowed by an argument, 'this' specefies the field. So
    this.hour = hour
    time.hour field = the hour argument of the constructor.

    see Using the this keyword for more information.
    You just got T4NK3D!
    Adonrcicg to rcesraeh the haumn mnid is clbapae of rdanieg tihs. -Jvaa Sun Moricyssemts-

  3. #3
    M77's Avatar
    M77
    M77 is offline Member
    Join Date
    May 2008
    Location
    LV
    Posts
    43
    Rep Power
    0

    Default

    example when "this" is usefull:

    public class Point1D {
    private double x;

    public double getX() {
    return x;
    }

    public void setX(double x) {
    this.x=x;
    }
    }

  4. #4
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    I was confused about this too (and still not entirely clear)

    But for example in my program we had 5 classes (one for calculations, one to hold one objects, another, etc)

    When I ran my program, the user's input wasnt being displayed properly and it turns out my constructor was like the one you posted without the 'this.' in front of it.

    From what I understand, because this '.this' wasnt there, the value stayed the same throughout the program when it shouldve only been set for that constructor "instance"

  5. #5
    JT4NK3D's Avatar
    JT4NK3D is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    You only really need to put 'this' if the field is shadowed. You could put 'this' if you want all the time even if the field isn't shadowed but it wouldn't make a difference. also, 'this' has another use for a constructor. Say you have two constructors, and you need one to call the other:
    Java Code:
    public class ThisTest {
             private int testField;
             public ThisTest() {
                  this(10);
                  // 'this(10)' calls the other constructor and passes the int parameter 10
                  // this is the other use of 'this'
             }
    
             public ThisTest(int testField) { // notice argument has same name as field; argument shadows field
                    this.testField = testField;
                    // to the testField field, assign the testField argument
                    // this shows the usual use of 'this'
             }
    }     // Scroll to right to see all the comments
    This class shows both ways of using the 'this' keyword. Hope it's clear now.
    Last edited by JT4NK3D; 05-31-2008 at 09:36 PM. Reason: typo
    You just got T4NK3D!
    Adonrcicg to rcesraeh the haumn mnid is clbapae of rdanieg tihs. -Jvaa Sun Moricyssemts-

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    this on Sun's java tutorial....
    freedom exists in the world of ideas

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 10:26 AM
  2. Replies: 5
    Last Post: 05-14-2008, 02:43 PM
  3. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 04:42 AM
  4. Replies: 1
    Last Post: 07-27-2007, 11:59 PM
  5. Replies: 1
    Last Post: 07-16-2007, 07:32 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
  •