Results 1 to 13 of 13

Thread: the word "this"

  1. #1
    vendetta is offline Member
    Join Date
    Jan 2010
    Posts
    31
    Rep Power
    0

    Default the word "this"

    I see the term, "this" used here and there and I don't understand what it means. I have searched around and has been said it means the current item. Well, I understand current item when using a cursor traversing through a linked list and how it stops at the current item. maybe that's it? Even if I'm hitting the nail on the head here, I don't understand how "this" is used. I have seen "this" used in two ways.

    one way was something like this: this.xyz = xyz;

    another way I am seeing it in my book in the binary trees chapter is this:

    {
    right = right.removeRightmost( );
    return this;
    }
    Last edited by vendetta; 02-25-2010 at 10:15 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default

    Do you understand it if you substitute 'my' (or 'me') for 'this'?

    kind regards,

    Jos

  3. #3
    vendetta is offline Member
    Join Date
    Jan 2010
    Posts
    31
    Rep Power
    0

    Default

    so far I understand "my" like this:

    say I have a class called "AClass", and I have a separate driver file called "MyDriver":

    AClass MyDriver = new aClass();

    and I'm using the test file like this

    MyDriver.add();
    MyDriver.addMoreStuff();

    MyDriver is the example of "my" that I know of.

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    This seems familar... Check out this thread to see if it helps

    this keyword


    as for your second example
    Java Code:
    {
         right = right.removeRightmost( );
         return this;
    }
    In this case, the keyword this would mean the current object(class instance) being worked in.

  5. #5
    vendetta is offline Member
    Join Date
    Jan 2010
    Posts
    31
    Rep Power
    0

    Default

    Quote Originally Posted by StormyWaters View Post
    This seems familar... Check out this thread to see if it helps
    this keyword
    I have read the thread, and still don't know what it is referring to.



    Quote Originally Posted by StormyWaters View Post

    as for your second example
    Java Code:
    {
         right = right.removeRightmost( );
         return this;
    }
    In this case, the keyword this would mean the current object(class instance) being worked in.
    "The current object... I don't know what the current item or current object is. can you give me an example?

  6. #6
    vendetta is offline Member
    Join Date
    Jan 2010
    Posts
    31
    Rep Power
    0

    Default

    what would be a current item in this program?

    can the "this" keyword be substituted anywhere in here?

    Java Code:
    public class MyDriver {
    
    	public static void main(String[] args) {
    
    		AClass MyDriver = new AClass(6,5);
    
    		MyDriver.addItem(5);
    		MyDriver.addItem(10);
    		MyDriver.addItem(4);
    		
    		MyDriver.printItems();
    	}
    }


    Java Code:
    public class AClass {
    
    	int a,b;
    	private int[] data = new int [100];
    	int index = 0;
    	
    	public AClass(int inA, int inB){
    		
    		a = inA;
    		b = inB;	
    	}
    	
    	
    	public void addItem(int n){
    		
    		data[index]=n;
    		index++;
    	
    	}
    	
    	
    	public void printItems(){
    		
    		
    		for (int i=0; i<index; i++){
    			
    			System.out.println(data[i]);
    			
    		}
    	}	
    }
    the output of the program above is:

    5
    10
    4

  7. #7
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    From what I have been told at uni is that "this" determines between the variables. Let me explain:

    Java Code:
    public class Course
    {
    	private String courseName, courseId;
    	private Module [] modules;
    
            	public Course(String courseName, String courseId, Module [] modules)
    	       {
                     
    
    		this.courseId=courseId;
    		this.courseName=courseName;
    		this.modules=modules;
    	        }
    
    }
    As you can see my variables are named the same as variables in my Course constructor. Now this is weird cause we all know that you cannot declare a variable with the same datatype or name twice. So when I say "this", i mean "Variable in THIS CLASS.<variable name> = this local variable received by the constructor.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  8. #8
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    "this" can only be used in instance methods or constructors.

    Instance methods are basically methods without "static" in them.
    Constructors are methods which create an object(instance) of a class.

    So in your class the constructor is:

    Java Code:
    public AClass(int inA, int inB){
     a = inA;
     b = inB;	
    }
    So when the constructor is called with following code:

    Java Code:
    AClass MyDriver = new AClass(6,5);
    It creates a variable "MyDriver" of the type AClass which consists of:

    int a = 6
    int b = 5
    an integer array of length 100
    int index = 0

    Such a variable is called "object" or "instance of a class"(in your case it's an instance/object of AClass).

    And there is a nice Sun explanation of what "this" is used for. You can find it here:
    Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  9. #9
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    "this" can also be used in place of a body of a constructor if you have one fully qualified constructor.

  10. #10
    vendetta is offline Member
    Join Date
    Jan 2010
    Posts
    31
    Rep Power
    0

    Default

    here is what I learned from the sun link:
    Java Code:
        public class Point {
            public int x = 0;
            public int y = 0;
        	
            //constructor
            public Point(int a, int b) {
        	x = a;
        	y = b;
            }
        }
    
    but it could have been written like this:
    
        public class Point {
            public int x = 0;
            public int y = 0;
        	
            //constructor
            public Point(int x, int y) {
        	this.x = x;
        	this.y = y;
            }
        }
    the comparison makes sense, sounds like it is an option either way.

    any thoughts on what "return this" could be here?

    Java Code:
       public BTNode<E> removeLeftmost( )
       {
          if (left == null)
             return right;
          else
          {
             left = left.removeLeftmost( );
             return this;
          }
       }

  11. #11
    Dumisan's Avatar
    Dumisan is offline Member
    Join Date
    Feb 2010
    Location
    Romania
    Posts
    30
    Rep Power
    0

    Default

    it seens you understand the use of "this" for constructors where refers to the local varialbe of the constructor that tacks the value of the variable you use it when creating a new object

    Quote Originally Posted by vendetta View Post
    any thoughts on what "return this" could be here?

    Java Code:
       public BTNode<E> removeLeftmost( )
       {
          if (left == null)
             return right;
          else
          {
             left = left.removeLeftmost( );
             return this;
          }
       }
    that return statement returns the courent values or position of the object
    StormyWaters allready explained that to you :)

    well look at your function type it needs to return an object, more precisely the courent object that is been used so this is what
    Java Code:
     return this;
    does
    silence i'm trying to meditate:p

  12. #12
    vendetta is offline Member
    Join Date
    Jan 2010
    Posts
    31
    Rep Power
    0

    Default

    what is a current item? in a method there are several values changing. is a current item the last one that was changed? is it the line above "return this"?

    is there a current item in the method below? or does a method need to return something to have something labeled as current?

    Java Code:
    public void method1(int first){
    
    int number;
    
    number = first;
     
    }

  13. #13
    Dumisan's Avatar
    Dumisan is offline Member
    Join Date
    Feb 2010
    Location
    Romania
    Posts
    30
    Rep Power
    0

    Default

    no is not like that
    the method you posted there is declared as void so it can not return something is just a pice of cod that will be executed evrytime you will call it
    that preview method of yours well that one require something to return and it needs to be the same type as the metho type wich meant an object namely the current object which is used

    an object is created when you call the constructor method let's say you have a class :
    Java Code:
    class MyClass {
    int var1;
    int var2;
    MyClass(int var1, int var2){    //this is the constructor
    this.var1 = var1;   //in this case this is used to indicate which var1 is refered   too 
    this.var2 = var2;
    }
    public print() {
    System.out.println("you have created an object with: "+ var1+","+var2);
    }
    //now in main method we will creat an object of this class by useing the constructor method
    public static void main(String args[]) {
    MyClass obj = new MyClass(5,6); //this is the part when you creat an object
    obj.print(); //here is how to use his method 
    }
    }
    i tryed to make it as simple as possible to show what is an object
    you cannot return an object by returning his variables values becous they are not an object type so when you need to return an object either way you use
    Java Code:
     return this;
    refering to the courent object created or if you have more then 1 objects created you can return the object name

    my advice is to read carefully what is object oriented programming and understanding the concepts behind them
    silence i'm trying to meditate:p

Similar Threads

  1. Reserved word "throws"
    By Lil_Aziz1 in forum New To Java
    Replies: 5
    Last Post: 01-02-2010, 03:12 PM
  2. Count lines cointaining "word" in input file
    By gwithey in forum New To Java
    Replies: 5
    Last Post: 04-02-2009, 06:23 AM
  3. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  5. Replies: 1
    Last Post: 05-07-2008, 02:19 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
  •