<p>The answers to these questions vary with the specific OO language. Here are some fairly generic answers.</p>
<ol>
<li><p>Can methods contain free variables?</p>
<p>In general yes.</p>
</li>
<li><p>Are free variables statically-scoped?</p>
<p>It's complicated. As a general rule (with many exceptions), most variables are lexically-scoped, except that the self reference (e.g., <code>this</code> in Java) is  an implicit, dynamically-scoped variable. Many OO languages further rewrite function calls <code>m()</code> to a method invocation <code>this.m()</code> if <code>m</code> is known to be a method of the type of <code>this</code>.</p>
</li>
<li><p>Do method names live in a different name space than variables?</p>
<p>Yes. Method names are <em>not</em> variables: they may not be freely <span class="math-container">$\alpha$</span>-renamed, since the actual text of the name constitutes part of the interface of an object. They are much more closely related to record labels or module component names. However, method names can interact with binding structure since many OO languages treat method invocations a bit specially (e.g., my answer to #2).</p>
</li>
</ol>

CC BY-SA 3.0
2013-04-02T12:04:48.270Z
Thanks for the quick answer, Neel.  Regarding Point 1, it seems to me that free variables in an OOL are mostly restricted to instance variables and class variables.  So disregarding `self` or `this`, as long as the instance relation and the inheritance chain are maintained, there is no danger of getting into dynamic scope, right?  Regarding Point 3, I do not quite get what you mean by "interacting with binding structure", could you clarify a bit?  Thanks.
day
CC BY-SA 3.0
2013-04-02T12:45:32.283Z
Almost all OO languages have local variables, and many also include first-class procedures (or objects) -- these are scoped lexically. If you disregard `this`, you don't really have an OO language, though! (Point 3 is nothing more than the fact that some OO languages let you shadow method names within a procedure by binding a local variable of the same name. Nothing hard is going on, though the details can get complicated/tedious.)
Neel Krishnaswami
CC BY-SA 3.0
2013-04-02T13:10:25.413Z
Right, these local variables (instance variables, class variables are kinda global variables though) are lexically scoped because they are contained in the object, and by following `this`, they can be reached.  Thanks for the further explanation.
day
CC BY-SA 3.0
2013-04-02T11:25:36.083Z
2013-04-02T11:25:36.083Z
2020-06-17T09:38:08.363Z
Neel Krishnaswami
<p>I was not a big fan of Object-Oriented Languages (OOL), but recently started to learn a bit more about their pros and cons in a general setting instead of diving into one such language.  I have a few questions here:</p>

<ol>
<li><p>Do methods in an OOL contain free variables?  If they do, are they statically-scoped?</p></li>
<li><p>Do method names live in a different name space than variables?</p></li>
</ol>

<p>For both questions, I have kinda guess answers but am not quite sure.  However, I would like to know more about the reasons behind and consequences of OOLs' specific answers to these questions.</p>

CC BY-SA 3.0
2013-04-02T15:07:19.233Z
probably better for [cs.se](http://cs.stackexchange.com)
vzn
<pl.programming-languages><object-oriented>
A few questions about Object-Oriented Languages in general
day
# A few questions about Object-Oriented Languages in general

(Asked by: username_0 on 2013-04-02)

I was not a big fan of Object-Oriented Languages (OOL), but recently started to learn a bit more about their pros and cons in a general setting instead of diving into one such language. I have a few questions here:

1. Do methods in an OOL contain free variables? If they do, are they statically-scoped?
2. Do method names live in a different name space than variables?

For both questions, I have kinda guess answers but am not quite sure. However, I would like to know more about the reasons behind and consequences of OOLs' specific answers to these questions.

**Tags:** pl.programming-languages, object-oriented

> **Comments:**
> **username_1**: probably better for [cs.se](http://cs.stackexchange.com)  

---

## 1 answers

### Answer by username_2 on 2013-04-02. Score: 3 | Accepted answer

The answers to these questions vary with the specific OO language. Here are some fairly generic answers.

1. Can methods contain free variables?

   In general yes.
2. Are free variables statically-scoped?

   It's complicated. As a general rule (with many exceptions), most variables are lexically-scoped, except that the self reference (e.g., `this` in Java) is an implicit, dynamically-scoped variable. Many OO languages further rewrite function calls `m()` to a method invocation `this.m()` if `m` is known to be a method of the type of `this`.
3. Do method names live in a different name space than variables?

   Yes. Method names are *not* variables: they may not be freely $\alpha$-renamed, since the actual text of the name constitutes part of the interface of an object. They are much more closely related to record labels or module component names. However, method names can interact with binding structure since many OO languages treat method invocations a bit specially (e.g., my answer to #2).

> **Comments:**
> **username_0**: Thanks for the quick answer, Neel.  Regarding Point 1, it seems to me that free variables in an OOL are mostly restricted to instance variables and class variables.  So disregarding `self` or `this`, as long as the instance relation and the inheritance chain are maintained, there is no danger of getting into dynamic scope, right?  Regarding Point 3, I do not quite get what you mean by "interacting with binding structure", could you clarify a bit?  Thanks.  
> **username_2**: Almost all OO languages have local variables, and many also include first-class procedures (or objects) -- these are scoped lexically. If you disregard `this`, you don't really have an OO language, though! (Point 3 is nothing more than the fact that some OO languages let you shadow method names within a procedure by binding a local variable of the same name. Nothing hard is going on, though the details can get complicated/tedious.)  
> **username_0**: Right, these local variables (instance variables, class variables are kinda global variables though) are lexically scoped because they are contained in the object, and by following `this`, they can be reached.  Thanks for the further explanation.  

---

