Java Naming Conventions and First Program
Let us begin to write the most basic code to exist in any coding language; that is the “ Hello World” program.
From here we begin to understand the coding structure of java language. for that, Let us have a look here.
Code in Java
Remember guys, Java is a case sensitive language.
Here we are writing a program to print Hello World on the output screen.
- The class is named as Main. The outer class can only be either public or default. We make a class public so that it is accessible from outside ( by a JVM ).
- If there is a dot ( . ) after a class name, it indicates that We are accessing one of its static members.
Here if we look at the statement System.out.println(“Hello world”);
- System is a class.
- System.out indicates that we are accessing ‘ out ‘ which is a static reference variable/ pointer which represents an object.
- ‘ println ‘ is a member function of the object ‘ out ‘.[ ‘ println ‘ prints the output in a new line automatically while ‘ print ‘ prints the output in the same line. ]
Now if we look at the statement public static void main(String args)
- Here public is an access specifier that allows the main method to be accessible everywhere.
- Static helps the main method to get loaded without getting called by any instance/object.
- Void clarifies that the main method will not return any value.
- Main is the name of the method.
- String args: Here we are defining a String array to pass arguments at command line. args is the variable name of the String array. It can be changed to anything such as String  a.
Java Naming ConventionsWe have some naming Conventions for all the components of java. Java naming conventions are moderate rules to decide the way in which identifiers such as classes, packages, methods, etc are to be written. These are not strict ‘ rules ‘ that are necessary to be followed; hence are called conventions.
These naming conventions help the user to understand the code better since one is able to differentiate among the various identifiers.
Class and Interfaces:
- Class names are nouns, in which the first letter of each internal word is capital. This type of representation is also known as the camel case.
- The use of whole words is preferred rather than abbreviations and acronyms ( except in the case where the abbreviation is more widely used than the actual word ).
- The interface names should also be capitalized just like done in classes.
- The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org.
- If the name contains multiple words, it should be separated by dots (.) such as java.util, java.lang.
- Subsequent components of the package name vary according to an organization’s own internal naming conventions.
- Variables: Variable names should be short yet meaningful.
- It starts with a lowercase letter. The usage of One- character variable should be avoided. Although permitted, initializing variable names with either underscore(‘_’) or dollar sign ‘$’ characters should not be done.
- Methods should be verbs, in mixed case with the first letter lowercase, and with the first letter of each internal word capitalized.
- The constant variables should all be in upper case with the words separated with the help of an underscore ‘ _ ‘.
- It may contain digits but not as the first letter.