I'm trying to understand the this keyword in java. I wanted to rewrite this code by using the this keyword instead. Please let me know if I've done it right. Here's the original code:
public class Book {
private String title;
private String author;
private String publisher;
public Book(String bookTitle, String authorName, String publisherName){
title = bookTitle;
author = authorName;
publisher = publisherName;
}
}
And here's the re-written code:
public class Book {
private String title;
private String author;
private String publisher;
public Book(String title, String author, String publisher){
this.title = title;
this.author = author;
this.publisher = publisher;
}
}
Have I done it correctly?
Thanks,
Kevin
EDIT: Thanks for the responses... one more question: in the constructor of the revised code, which side of the equals sign refers to the class variables? For example, in this.title = title;, does this.title refer to the title variable from the constructor or from the class variable?
Based on the responses below, I think the answer is this.title refers to the class variable title.
title = titlehas no sence.