8 Replies - 543 Views - Last Post: 10 November 2017 - 10:06 AM Rate Topic: -----

#1 Frenchi33  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 20-November 16

What does "this" mean?

Posted 09 November 2017 - 04:52 PM

Hi guys, so I'm gonna have to make the transition from Python to Java for school, and I'm trying to understand how a constructor works. I've made a simple program and I'm fairly positive that I understand it, but am wondering what the word "this" means. I see it all the time, but don't know what it does. I assumed it was kind of like the word "self" in Python which is why I used it, but after doing some reading, there seems to be a lot of uses for "this", and I didn't quite understand what I was reading. Can you guys please explain it to me?

Also, one more thing... Does java use different words for Instance and Attribute?

Thanks, in advance!

class Person 
{
	// declare attributes
	String firstName;
	String lastName;
	
	// initialize attributes
	public Person(String firstName, String lastName) 
	{
		this.firstName = firstName;
		this.lastName = lastName;
	} // end constructor
	
	public String getFullName() 
	{
		return this.firstName + " " + this.lastName;
	} // end getter
	
	public void setFullName(String newFirstName, String newLastName) 
	{
		this.firstName = newFirstName;
		this.lastName = newLastName;
	} // end setter
	
} // end class Person
/* ---------------------------------------------------------------------- */
public class Practice 
{
	public static void main(String[] args) 
	{
		// create and display instance
		// similar to 'Scanner input = new Scanner(System.in);'
		Person p1 = new Person("Jackie", "Chan");
		System.out.println(p1.getFullName());
		
		// re-name and display instance
		p1.setFullName("Bob", "Marley");
		System.out.println(p1.getFullName());
	} // end method main
	
} // end class Practice


This post has been edited by Frenchi33: 09 November 2017 - 05:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: What does "this" mean?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5073
  • View blog
  • Posts: 13,697
  • Joined: 18-April 07

Re: What does "this" mean?

Posted 09 November 2017 - 05:08 PM

It means the current instance of the class.

http://javabeginners...eyword-in-java/

When you say this.firstName you are saying the current instance's version of the firstName variable. :)
Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: What does "this" mean?

Posted 09 November 2017 - 05:09 PM

From what I know of python, this and self are the same thing. The difference between the languages is that in java that is an implicit this. You don't have to declare it in the method signature.

An instance is just an object. For example, you could create several instances of the type String.

Attribute probably just means instance variable.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12148
  • View blog
  • Posts: 45,161
  • Joined: 27-December 08

Re: What does "this" mean?

Posted 09 November 2017 - 05:21 PM

The obligatory and excellent DIC tutorial on the this keyword. :)

http://www.dreaminco...eyword-in-java/
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10682
  • View blog
  • Posts: 18,296
  • Joined: 19-March 11

Re: What does "this" mean?

Posted 09 November 2017 - 10:11 PM

The main difference between "this" and "self" is an implementation detail: in python, self is a local variable referring to the instance to which a function is bound. To the function, it looks like just another variable. In Java, this is a keyword in the language.

Apart from some language-specific details, they mean the same thing.

(When you get to javascript, you will find that there is also a this keyword which is somewhat more confusing, largely because javascript is a crap language written presumably in a drug-fueled frenzy over a longer-than-average weekend, and makes about as much sense as some of Philip K. Dick's minor novels like The Zap Gun, also the product of a viciously compressed development time scale and, admittedly, in Dick's case, somewhat more amphetamine than one would normally consider "healthy" or even "survivable" which in all honestly is probably not what Brendan Eich used for his main source of inspiration, although you really couldn't tell from the final product. But I digress.)
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,765
  • Joined: 12-December 12

Re: What does "this" mean?

Posted 09 November 2017 - 11:43 PM

Quote

Also, one more thing... Does java use different words for Instance and Attribute?

Instances are the same, Java prefers the term members to attributes.
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3444
  • View blog
  • Posts: 15,605
  • Joined: 20-September 08

Re: What does "this" mean?

Posted 10 November 2017 - 03:23 AM

Quote

Java prefers the term members to attributes.

Hmm - not sure about that ;) 'Member' sounds more C++ to me. Often denoting member variables by prepending the nasty m_
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,765
  • Joined: 12-December 12

Re: What does "this" mean?

Posted 10 November 2017 - 03:46 AM

With OOP it depends who we ask ;)

object-oriented programming :wikipedia

In more formal OOP texts I recall they tend to talk about data members and member functions. Collectively, though, they can also be referred to as members, as in instance members or class members. (When we type a dot after an object in an IDE, what is listed with intellisense I am used to referring to as the members, or member list; perhaps this is MS specific usage?)

From wikipedia:

Quote

Procedures in object-oriented programming are known as methods; variables are also known as fields, members, attributes, or properties. This leads to the following terms:
  • Class variables belong to the class as a whole; there is only one copy of each one
  • Instance variables or attributes data that belongs to individual objects; every object has its own copy of each one
  • Member variables refers to both the class and instance variables that are defined by a particular class
  • Class methods belong to the class as a whole and have access only to class variables and inputs from the procedure call
  • Instance methods belong to individual objects, and have access to instance variables for the specific object they are called on, inputs, and class variables

OOP has never been known for the clarity of its terminology :)
Was This Post Helpful? 0
  • +
  • -

#9 Frenchi33  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 20-November 16

Re: What does "this" mean?

Posted 10 November 2017 - 10:06 AM

View Postcfoley, on 09 November 2017 - 05:09 PM, said:

You don't have to declare it in the method signature.

So do you mean I don't need it in the getFullName() and setFullName() methods? Like this?

class Person 
{
	// declare variables
	String firstName;
	String lastName;
	
	// initialize object
	public Person(String firstName, String lastName) 
	{
		this.firstName = firstName;
		this.lastName = lastName;
	}
	
	// combine first and last name
	public String getFullName() 
	{
		return firstName + " " + lastName;
	}
	
	// assign new first and last name
	public void setFullName(String newFirstName, String newLastName) 
	{
		firstName = newFirstName;
		lastName = newLastName;
	}
	
}
/* ---------------------------------------------------------------------- */
public class Practice 
{
	public static void main(String[] args) 
	{
		// create object
		Person p1 = new Person("Jackie", "Chan");
		Person p2 = new Person("Bob", "Marley");
		
		// display full name
		System.out.println(p1.getFullName());
		System.out.println(p2.getFullName());
		
		// assign new first and last name
		p1.setFullName("Michael", "Jackson");
		p2.setFullName("Neal", "Schon");
		
		// display new full name
		System.out.println(p1.getFullName());
		System.out.println(p2.getFullName());
	}
	
}



... because it still works.

And thanks everyone for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1