Results 1 to 7 of 7
Thread: this() function
- 01-22-2011, 01:14 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
- 01-22-2011, 01:19 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
I think its
notJava Code:this.book = book
Here is the explanation from its tutorial.Java Code:book.this = book
Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
- 01-22-2011, 01:20 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
yeah sorry, made a mistake there :D
- 01-22-2011, 01:31 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
In that situation it's to differentiate between the two items,
say you make the contructer that takes string book as an argument.
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){ this.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.Java Code:Book(String book){ book = book; }
Didn't notice the link to the tutorial above, that will definitely have the best information.
- 01-22-2011, 06:40 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
You can only use this() (or this(argtype arg) etc) inside a class. It is used for calling other constructors of your class:
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 nameJava 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; } }
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.
- 01-23-2011, 03:15 AM #6
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
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]; } }
-
subith, please ignore my last post which I've deleted. I see that you're answering the original question with color-coded code. Very cleverly done. Thanks.
Similar Threads
-
Possible? Callback function passed as arguments to another function
By TreyAU21 in forum Advanced JavaReplies: 3Last Post: 12-04-2009, 03:08 PM -
function
By nanna in forum New To JavaReplies: 1Last Post: 11-17-2008, 09:20 PM -
Need help with get function
By calicocal in forum New To JavaReplies: 10Last Post: 11-09-2008, 07:59 PM -
Need a little help with a function!
By Nuluvius in forum New To JavaReplies: 3Last Post: 02-07-2008, 11:33 PM -
I want to add function
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks