Results 1 to 7 of 7

Thread: this() function

  1. #1
    sung is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default this() function

    String book;
    book.this()=book;

    what does this() do here exactly?

    thx guys

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I think its
    Java Code:
     this.book = book
    not
    Java Code:
     book.this = book
    Here is the explanation from its tutorial.
    Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  3. #3
    sung is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    yeah sorry, made a mistake there :D

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    In that situation it's to differentiate between the two items,
    say you make the contructer that takes string book as an argument.
    Java Code:
    Book(String book){
      this.book = book;
    }
    says the object being created has a variable called book and you want to set it to the input book.
    Java Code:
    Book(String book){
      book = book;
    }
    this would not run correctly, if you used this constructor it wouldn't set the variable and you would create a null object.

    Didn't notice the link to the tutorial above, that will definitely have the best information.

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You can only use this() (or this(argtype arg) etc) inside a class. It is used for calling other constructors of your class:
    Java Code:
    public class MyClass {
      private int[] someArray;
      private String someString;
      public MyClass() {
        someArray = new int[10];
      }
    
      public MyClass(String arg) {
        this(); //calls the no arg constructor
        someString = arg;
      }
    }
    As already explained, this keyword is used as a reference an object keeps of itself, it's mainly used to differentiate inner scope variables with the objects variables that have the same name
    Java Code:
    public class MyClass {
      private String someString;
      public MyClass(String someString) {
        //someString = someString; this line sets the string passed to the constructor
        //to itself, doesn't make any sense
        this.someString = someString; //this line sets the objects someString variable 
        //to the string passed to the constructor
      }
    }
    Ever seen a dog chase its tail? Now that's an infinite loop.

  6. #6
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    I have put colors which mention which "book" comes where. See if you understand. The rest everything is as mentioned in the previous comments.

    Java Code:
    class Book {
    	String [COLOR="Lime"][B]book[/B][/COLOR];
    	Book(){}
    	Book(String [B][COLOR="DeepSkyBlue"]book[/COLOR][/B]) {
    		this.[COLOR="Lime"][B]book[/B][/COLOR] = [B][COLOR="DeepSkyBlue"]book[/COLOR][/B];
    	}
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 3
    Last Post: 12-04-2009, 04:08 PM
  2. function
    By nanna in forum New To Java
    Replies: 1
    Last Post: 11-17-2008, 10:20 PM
  3. Need help with get function
    By calicocal in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 08:59 PM
  4. Need a little help with a function!
    By Nuluvius in forum New To Java
    Replies: 3
    Last Post: 02-08-2008, 12:33 AM
  5. I want to add function
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:25 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
  •