Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+6 votes

When do we not put self. in front of variable in a class?

asked in old_CSC201 by (1 point)

1 Answer

+2 votes

If a variable is local to a method in the class, then it is not an instance variable and you don't use self. in front of it.

For example, let's say I was writing an employee class that stored as an instance variable the employee's hourly pay rate. If the class had a method getPay with a parameter for the number of hours worked that week, the number of hours worked would not be stored as an instance variable so no self. Also, if in writing the computation statements I wanted to use a variable to temporarily store the number of overtime hours for that week so that I could compute the week's pay, the variable storing the number of overtime hours would be local to the method and so no self.

answered by (1 point)
...