Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the order of a Java class matter?

Is there any difference between the following? :

Example 1:

public class OddEven {

private static void OddEven() {
    //some calculation.
}

public static void main(String[] args) {
   OddEven();
}
}

Example 2:

public class OddEven {

private static void main(String[] args) {
   OddEven();
}

private static void OddEven() {
    //some calculation.
}
}

The reason I ask is I would always go with example 2, putting Main first. Though most of the examples I've seen online put the methods first, before Main.

I've never had formal computing lessons, and I apologise if this is an obvious question, but I'd like to know:

  • Is the order of the layout simply aesthetics, or convention?
  • Does it make any difference in processing efficiency and/or memory?
  • If so, is that saving seen for all programming languages?

Thanks for any help on this topic.

like image 492
user2514440 Avatar asked Feb 14 '14 12:02

user2514440


People also ask

What is the order of Java class structure?

First the public class variables, then the protected , then package level (no access modifier), and then the private . First public , then protected , then package level (no access modifier), and then private .

In what order should methods be in a class?

Member variables at the top of the class, then constructors, then all other methods. And you order the methods to be close together with how they are used within the class (rather than arbitrarily putting all public then private then protected).

In which order are the elements of a class typically arranged Java?

Class (static) variables: First the public class variables, then the protected, and then the private. Instance variables: First public, then protected, and then private. Methods: These methods should be grouped by functionality rather than by scope or accessibility.

Which is the most important class in Java?

The java. lang. Class is one of the most important class in Java and it can provide several utility methods like getClass(), forName() which is used to find and load a class.


2 Answers

No, there is no difference. Java doesn't care about the declaration order of methods in a class.

like image 150
Keppil Avatar answered Oct 14 '22 02:10

Keppil


The order does not matter. But there is a common courtesy to public methods and static methods at the beginning of a class. This is just a developers choice

like image 3
diazazar Avatar answered Oct 14 '22 03:10

diazazar