Question

In: Computer Science

what are the differences/similarity between the keyword "this" in java and the keyword "self" in python...

what are the differences/similarity between the keyword "this" in java and the keyword "self" in python and why or when should we use these two keywords? if you could provide a code example side by side that would be great!

thank you!

Solutions

Expert Solution

Thanks for A2A.

Similarities between self in python and this in java:

  • can be used to refer current class instance variable.
  • can be used to invoke current class method (implicitly)
  • can be used to invoke current class constructor.
  • can be passed as an argument in the method call.
  • can be passed as argument in the constructor call.
  • can be used to return the current class instance from the method.
  • can be used to remove name conflicts between instance variables and parameters.

Differences between self in python and this in java:

  • Technically both self and this are used for the same thing. They are used to access the variable associated with the current instance. Only difference is, you have to include self explicitly as first parameter to an instance method in Python, whereas this is not the case with Java.
  • self is not a keyword in Python. self is a parameter in function and the user can use a different parameter name in place of it. Although it is advisable to use self because it increases the readability of code.
    Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked.

If you have any doubts please comment.

Thank you.


Related Solutions

What are the differences and similarity between an intrapreneur and international entrepreneur?
What are the differences and similarity between an intrapreneur and international entrepreneur?
What are the similarity and differences between HiTrust CSF and SOC 2?
What are the similarity and differences between HiTrust CSF and SOC 2?
What are differences between connective and epithelial tissues? What is a similarity between nervous and muscle...
What are differences between connective and epithelial tissues? What is a similarity between nervous and muscle tissues?
What do you know about programming in Python? What is the difference between Python and Java?
What do you know about programming in Python? What is the difference between Python and Java? What does the term Open Source mean? Name four examples of Open Source software. What is the IDEL programming environment? How does IDEL relate to Python? How do you spread a long statement over multiple lines in Python? How do you use the loop-index? How will knowing and understanding Python impact what you do in your profession and/or personal experiences?
What are the differences between a literal and a variable in Java? Select one: A literal...
What are the differences between a literal and a variable in Java? Select one: A literal is an exact expression representing the symbolic meaning of a value, a variable allows changes to be made to the expression. A literal (or string literal) is a String value at a particular point in your program, a variable is a datatype that the literal can be stored under. A literal is a constant value that is initialised with the keyword final, a variable...
Using outside research sources, describe the similarity and differences between telehealth and telemedicine in the health...
Using outside research sources, describe the similarity and differences between telehealth and telemedicine in the health care provider workplace. What are the positive and negative aspects of telehealth nursing? What impact is telehealth reimbursement in the current environment having on the long term development and implementation of telehealth initiative within a physician provider or health care or hospital setting? What is your outlook for continued usage and success/failure of telehealth after the current pandemic slows? Support your theory with financial...
Using outside research sources, describe the similarity and differences between telehealth and telemedicine in the health...
Using outside research sources, describe the similarity and differences between telehealth and telemedicine in the health care provider workplace. What are the positive and negative aspects of telehealth nursing? What impact is telehealth reimbursement in the current environment having on the long term development and implementation of telehealth initiative within a physician provider or health care or hospital setting? What is your outlook for continued usage and success/failure of telehealth after the current pandemic slows? Support your theory with financial...
similarity and differences between tunneling technology roadheader, tunnel boring machine and drill and blast
similarity and differences between tunneling technology roadheader, tunnel boring machine and drill and blast
Explain the similarity between a hydrogen bond and a covalent bond. Explain a similarity between a...
Explain the similarity between a hydrogen bond and a covalent bond. Explain a similarity between a hydrogen bond and an electrostatic interaction. Explain a difference between an electrostatic interaction and a hydrogen bond.
Operating Systems: Describe in your own words the similarity or differences between any two desktop or...
Operating Systems: Describe in your own words the similarity or differences between any two desktop or mobile operating systems in terms of process management and file systems implementation. Mention proper references for your source of information. Please choose systems OTHER THAN iOS and Android. Thanks.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT