Naming Conventions in Java
Naming conventions are standard practices for class, interface, methods, etc. to identify and to distinguish from each other.
the class name should start from the upper case English alphabet.
example: Hello
interface the name should also start from the upper case English alphabet.
example: Demo
variables/objects/instances should start from the lower case.
example: apple
the method should start from the lower case.
example : mango()
constants should have all letters in uppercase and underscore should be used for separation of two words.
example: APPLE_BANANA
the package name should start with lower case
example: red
Companies use their reverse internet domain address for defining package names.
generally, the company uses the format :
extension.companyname.projectname.packagename.
if the company name is 'himanshupal' and extension is 'com' and let it be the name of the project is 'a1' and the package name is 'b1'.
than package will be :
com.himanshupal.a1.b1
internal company naming collisions need to be handled by company itself.
Camel case convention :
When two words are combines then the second word should start with upper like appleMango
Naming conventions are not rules that everyone has to follow. these are standard conventions that everyone should follow so as to increase the readability of programmers.
Here is a sample program showing naming conventions :
package hello; //declaration of package name
public class TestNamingConventions { //declaration of class name
private int age; //declaration of variables/objects/instances
private final static String CITY_INDORE; //declaration of constants
public void a(){ //declaration of the method name
System.out.println("Hi this is Himanshu");
}
public static void main(String[] args) {
TestNamingConventions obj = new TestNamingConventions();
obj.a();
}
}
Output :
Hi this is Himanshu
Advantages of naming conventions :
1) Understanding code becomes easy for other programmers.
2) We can easily distinguish methods, variables, constants, etc.
3) Coding is performed in a standard way.
No comments:
Post a Comment