Does the order of declaration matter in Java/C#?

2020-02-06 02:38发布

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#?

标签: java c#
12条回答
▲ chillily
2楼-- · 2020-02-06 03:23

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.

查看更多
劫难
3楼-- · 2020-02-06 03:24

These answers are conflicting which makes things difficult to understand. The best practice is to declare your methods, variables, etc. in a common sense chronological order so that there is no confusion and this is across all programming languages. Your main will always be first so that CAN be at the beginning or end, but still, you should start with main, and when you call a method in main it should be the next method after main and so on. This, at least to me, makes the most sense and makes the code the most readable (comments help a lot too, because let's face it, code is really gibberish). I'm not a coding expert, but I understand that the best practice with any algorithm is to break it down into as many simple steps as needed (please with comments). This doesn't 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 gibberish. Help yourself and anyone else that might look at your code and write it in a logical order no matter what language you use. (And again, please use comments! You'll thank me later.)

查看更多
Lonely孤独者°
4楼-- · 2020-02-06 03:26

No, the compiler does two passes.

查看更多
你好瞎i
5楼-- · 2020-02-06 03:28

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.

查看更多
疯言疯语
6楼-- · 2020-02-06 03:38

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

查看更多
霸刀☆藐视天下
7楼-- · 2020-02-06 03:39

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.

查看更多
登录 后发表回答