tags:

views:

450

answers:

10

In C++ I can't use a method if I declare it after the calling method.

Does this order matter in other languages like Java or C#?

A: 

I'm not sure about c#, but in java you can.

ddccffvv
+14  A: 

No

Dan Fish
Nothing like being short and succinct.
David Christiansen
No, would have been more then enough. It is afterall a Yes/No question.
chrissie1
The space before the period is unnecessary and really clutters the answer. I suggest removing it to prevent confusing people with unnecessary fluff.
William Brendel
Sometimes simple answers are the best ones
JaredPar
@William, yes this almost caused me to -1 instead of +1 the question. I gave the poster a little bit of slack though ;)
JaredPar
I'm struggling to get rid of that white space. The minimum response length after trimming is 10 (probably for good reason). My answer is actually No_______. I', thinking about posting a StackOverflow question to see if anyone can help me.
Dan Fish
A: 

It doesn't in C#.

Inferis
A: 

neither C# nor Java does.

codemeit
+2  A: 

In Java as well as in c# there is no separate method declaration.

The declaration of the method is done with its implementation. You also do not need to keep track of file includes so that the classes know about eachother as long as they are in the same namespace.

Aleris
A: 

The variable should be accessible in the method where it is being used. It does not matter if it is declared before or after the usage.

Developer_N
+10  A: 

Declaration order of methods never matters in C# or Java. Likewise it doesn't matter whether you declare a method before or after a variable that it uses.

Declaration order of variables can matter, however, when they're initialized one depends on another. For example (C#):

using System;

class Test
{
    static int x = 5;
    static int y = x;

    static void Main()
    {
        // Prints x=5 y=5
        Console.WriteLine("x={0} y={1}", x, y);
    }
}

but:

using System;

class Test
{
    static int y = x;
    static int x = 5;

    static void Main()
    {
        // Prints x=5 y=0
        Console.WriteLine("x={0} y={1}", x, y);
    }
}

Java prevents this exact situation, but it's easy to mimic:

public class Test
{
    static int y = getInitialValue();
    static int x = 5;

    public static void main(String args[])
    {
        System.out.println("x=" + x + " y=" + y);
    }

    static int getInitialValue()
    {
        return x;
    }
}

In C# things become even more confusing when you involve partial classes. Initialization occurs in textual order in C#, but that order isn't fully defined when you have multiple files contributing to the same class.

Needless to say, avoid this wherever you possibly can!

Jon Skeet
+1  A: 

For Java, the authoritative answer is hidden in Chapter 1 (Introduction) of the Java Language Specification ("JLS," 3rd edition, viewable online for free):

Declaration order is significant only for local variables, local classes, and the order of initializers of fields in a class or interface.

Peter Dillinger
A: 

No, the compiler does two passes.

fortran
A: 

There is one tricky case where lexically the function to be called can be declared after the point of call, but not semantically. This is because the class is deemed to be completely defined in the body of the class member functions.

$9.2.2 - "A class is considered a completely-defined object type (3.9) (or complete type) at the closing } of the class-specifier. Within the class member-specification, the class is regarded as complete within function bodies, default arguments and constructor ctor-initializers (including such things in nested classes). Otherwise it is regarded as incomplete within its own class member-specification."

struct A{
    void f(){g();}    // OK to call 'g' even if the compiler has not seen 'g' as yet
    void g(){};
};

int main(){
    A a;
    a.f();
}
Chubsdad