[ACCEPTED]-Does the order of declaration matter in Java/C#?-c#

Accepted answer
Score: 32

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

Declaration order 10 of variables can matter, however, when they're 9 initialized one depends on another. For 8 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 7 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 6 more confusing when you involve partial 5 classes. Initialization occurs in textual 4 order in C#, but that order isn't fully 3 defined when you have multiple files contributing 2 to the same class.

Needless to say, avoid 1 this wherever you possibly can!

Score: 27

No .

0

Score: 5

No, the compiler does two passes.

0

Score: 2

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

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

Score: 1

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

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

Score: 0

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

0

Score: 0

It doesn't in C#.

0

Score: 0

neither C# nor Java does.

0

Score: 0

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

Score: 0

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

$9.2.2 - "A class is considered 8 a completely-defined object type (3.9) (or 7 complete type) at the closing } of the 6 class-specifier. Within the class member-specification, the 5 class is regarded as complete within function bodies, default 4 arguments and constructor ctor-initializers (including 3 such things in nested classes). Otherwise 2 it is regarded as incomplete within its 1 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();
}
Score: 0

The order of methods/constructors does matter 1 in Java in some corner cases:

class Callee {
    private static void bar(int i) { } // compilation error if first
    public static void bar(String s) { } // ok if this method is first
}

class Caller {
    private void foo() { Callee.bar(bar()); }
    private <T> T bar() { return null; }
}
Score: 0

These answers are conflicting which makes 23 things difficult to understand. The best 22 practice is to declare your methods, variables, etc. in 21 a common sense chronological order so that 20 there is no confusion and this is across 19 all programming languages. Your main will 18 always be first so that CAN be at the beginning 17 or end, but still, you should start with 16 main, and when you call a method in main 15 it should be the next method after main 14 and so on. This, at least to me, makes the 13 most sense and makes the code the most readable 12 (comments help a lot too, because let's 11 face it, code is really gibberish). I'm 10 not a coding expert, but I understand that 9 the best practice with any algorithm is 8 to break it down into as many simple steps 7 as needed (please with comments). This doesn't 6 make sense:

   final List<int[]> intArrays = Arrays.stream(testarray).collect(Collectors.toList());
   final List<Integer> integers = intArrays.stream().flatMap(z -> 
                  Arrays.stream(z).boxed()).collect(Collectors.toList());

unless you add comments like:
final List<int[]> intArrays = Arrays.stream(testarray).collect(Collectors.toList());
// this makes a List (of ints) variable type that cannot be changed based on the stream of 
//(the testarray in this case) and uses the Collector method to add the ints
//to intArrays (our variable name)
final List<Integer> integers = intArrays.stream().flatMap(z -> 
                  Arrays.stream(z).boxed()).collect(Collectors.toList());
// I would have to look this up, because I honestly have no clue what it does exactly. 

Like I said, code is basically 5 gibberish. Help yourself and anyone else 4 that might look at your code and write it 3 in a logical order no matter what language 2 you use. (And again, please use comments! You'll 1 thank me later.)

More Related questions