The short answer, as Johannes said, is nearly always #2. Both will only exist in memory in a single location, but instance methods have context (which is basically an extra hidden argument) that gives them access to the instance.
But: Some languages/environments will instantiate a function that looks (to a naive reading of the source code) like one function multiple times. JavaScript is a good example of this: The function object closes over the scope in which it's defined, so if there are multiple scopes (such as a function being called more than once), multiple distinct function objects result. (Interpreters/JIT-ers may optimize whether the code is duplicated, but conceptually it is and the function references differ.)
Here's an example in JavaScript:
function foo() {
alert("hi there");
}
function buildBar(x) {
function bar() {
alert(x);
}
return bar;
}
var f1, f2;
f1 = foo;
f2 = foo;
f1();
// alerts "hi there"
f2();
// alerts "hi there"
alert("f1 === f2 ? " + (f1 === f2));
// alerts "true", there is only one `foo`
var b1, b2;
b1 = buildBar("one");
b2 = buildBar("two");
b1();
// alerts "one"
b2();
// alerts "two"
alert("b1 === b2 ? " + (b1 === b2));
// alerts "false", there are two `bar`s
This becomes relevant in certain implementations of "private" instance data in JavaScript, where people define the instance methods within the constructor (much as buildBar
defines bar
) so they have access to private variables declared within the constructor. This has the desired effect (private instance data), but the impact of multiple function instances -- one for each instance of the object.