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:
Thanks for any help on this topic.
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 .
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).
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.
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.
No, there is no difference. Java doesn't care about the declaration order of methods in a class.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With