FAQ CS1 Week 7

Muddiest Points from Lecture Material

  1. What does "this" mean in Java?

    In Java, this is a placeholder used within a class definition to represent the object that the instance method was invoked on. If you think about it, the person who wrote the class implementation has NO WAY of knowing what identifiers a user might come up with when using a class, and if the user has instantiated more than one object of the same class, the implementer would have no way of talking about the current object without this! In such a situation, this may be seen followed by a dot (.). Then, it refers to the object that received the message that caused that instance method to be invoked, and may be used to gain access to one of the receiver object's instance variables. (For example, see the distanceTo( int x, int y ) or distanceTo( int x, int y ) method in our Point class, linked below for sections 10 and 14, respectively.) "this" may only be used in such a way in INSTANCE methods, not in class methods. This (:-)) rule should make sense because there is no object involved when a class method is invoked.

    If this is seen in a constructor followed by a "(", it is a reference to another constructor within this same class definition. If used, it must be the FIRST line of that constructor.

    For examples of the use of this, see the implementation of the Point class for your particular section from http://www.cs.rit.edu/~ncs/Courses/cs1/Point/. It contains a heavily annotated version of the Point class which we wrote in your class.

  2. Why do we call a constructor from within a constructor using this rather than using the constructor's real identifier, e.g., Point()?

    That's a really interesting question. I believe this has to do with the fact that when we instantiate a variable by saying

    Point p = new Point();
    there is actually some stuff going on that isn't seen in the implementation of the constructor, i.e., memory space is being allocated to store the state of the Point (e.g., the instance variables). So, if we were to invoke another constructor from within another one by using its "real name", I could imagine that we could run into trouble with memory being allocated twice! I suspect that's why we have this special name to use. Its syntax likely says: "Don't allocate more memory; just execute the statements in the implementation."

  3. The concept of scope confuses me. For example, yesterday when we learned about s.a and s.b, I didn't understand how they came into the picture.

    I am linking the Scope directory here so that you can download Scope.java, ScopeTest.java, and the output. I would suggest that you try running the programs and see if you understand why the output is what it is. You might also try changing the Scope.java program a bit and see how your changes affect the output.

    Scope in this sense is just a class I created to show you more about the concept of scope. The concept of scope has to do with which identifiers are known where and what name we have to use to access an individual identifier. A related concept is the lifetime of an identifier, i.e., how long a particular identifier is in existence. For example, variables that are local to a method are only "alive" for as long as that method is executing.

    So, if you create an object and have access to its instance variables like we did in the Scope example (because the instance variables a and b were public), you are able to access them from your program by specifying the object_name.instance_variable_name. In our case, we had an instantiated Scope object called s. All instantiated Scope objects have instance variables called a and b. To access them directly in our ScopeTest program, we must refer to them as s.a and s.b.

    I hope you recognize that the code in both Scope and ScopeTest is not really good code. I purposefully did not use meaningful variable names to demonstate this concept. If you can trace and understand (bad) code like this, you will be a very valuable employee indeed! :-)


Nan C. Schaller
Rochester Institute of Technology
Computer Science Department
102 Lomb Memorial Dr.
Rochester, NY 14623-5608
telephone: +1.585.475.2139
fax: +1.585.475.7100
e-mail: ncs@cs.rit.edu
September 30, 2004
http://www.cs.rit.edu/~ncs/Courses/cs1/Muddy7.shtml