Skip to main content

Naming Conventions in Java

Naming Conventions in Java

As the movies are rated G, PG, U, A depending on the age restriction and by just gazing through the rating our parents decide the movie to watch together, or a book with diffrent labels to keep track of the sections and just by seeing them we know what they represent, the same labeling method has been introduced by Java team to diffrenciate variables, methods, classes, constants etc.. and we will be going through them all here.
Type Naming Convention Example
Variables Must start with a lower case and every other word capitalized int counter;
Constants Must all be upper case and _ for diffrenciating every other word final int MAX_HEALTH = 100;
Methods Must start with a lower case and every other word capitalized.
can be distingiused from the variable by the () next to the method name
void printNum(){..}
Class Must start an upper case letter and every other word must be capitalized class HelloWorld{..}
Interface Must start an upper case letter and every other word must be capitalized interface ActionListner{..}
  1. Packages

    A package must all be in lowercase letters example java.awt and as it represents a group of classes and interface in case another developer wants to import your package this nameing convention will be helpful for him to proceed further without worring about the case in the name for example java.awt is easier to recall than jAvA.AwT so it avoids the un necessary confusion.

    here is an example
    package myown.io import java.io

  2. Constants

    A constant variable must be only completly upper case and to diffrenciate the words we can use underscores(_), there are some pre-buid constants such as MAX_VALUE , MIN_VALUE in the Number's subclasses etc..

    here is an example
    final int WORKING_HOURS = 6;

  3. Variables

    A variable is a refrence of an object or a value of any datatype and the name must start with lowercase and every other word must be capitalized and this method is called cammel casing for example int ageOfPet = 1; or if its only one word then int age = 10; so anytime you see this naming convention then assume its a variable unless except with an exception that we will see in the methods.

    here is an example int age = 6; age = 12; System.out.println(age);

  4. Methods

    The naming convention is the same as the variable, must start with the lowercase and every other word must be capitalized or in other words must be a camel case word but I know you are asking in your mind "if both methods and variables have the same naming convention whats the whole point?"  Well, the thing is methods always have brackets next to them example sum(5+5) or public static void main(String[], arg) so as you can see even though they have the same naming convention they both can be easily differentiated.

    here is an example
    void printNum(int a)
    {
    System.out.println(a)
    }

  5. Class

    A class must start with a capital letter and every other word capitalized for example class HelloWorld {} or class Hello{} so just capitalize all the words, the same naming convention for the abstract classes as well.

    here is an example
    class HelloWorld
    {
    ....
    }

  6. Interface

    An Interface must be named same as the classes naming convention, The first letter must be capitalized and all other words must be capitalized as well for example interface ActionListner{}

    here is an example
    interface ActionListner
    {
    ....
    }

Other Important Note

  • Symbols such as $ or _ etc can be used if you want but everything must start with an alphabet
  • Not following the naming convention will not throw compile time error.

Comments

Popular posts from this blog

Designer PDF Viewer - HackerRank Problems

Difficulty: EASY Problem : The objective here is to find the size of the highlighted area, and we are given the size's of all the alphabets, we have to find the largest alphabet in the highlighted word and then calculate the size of the rectangle so if the tallest character is 3 then the size of the box will be 3 * number of characters given. Visual representation of the selection : abc def ghij Inputs An array with the sizes of all alphabets a-z in order. A String of highlighted words. Important points to note The array which holds the height of each character in ascending order which means the arrays 0th index will have the height of a 1st index will have the height of b and so on and so forth in the end the hight of z will be there so it's easy to locate each character. A String with the highlighted word. This means we have got the characters inside the rectangle, all we have to find is ...

Literals of Base numbers in Java ( Octal , Hexadecimal, Decimal)

1. Overview: A literal key indicates the compiler how to interpret the value of the given data type, for numbers we can calculate the value by using Octal representation or hexadecimal representation but just typing out a hexadecimal value to an int will throw us an error because the compiler has no idea how to handle it but if we assign the java specified prefix for the required bases with some literals then the compiler will not throw us any error as it understands how to interpret the value.  Base Litrals Values Example Eg. Value Decimal none 0-9 int x = 10; x is 10 Octal 0 (zero) as the prefix 0-7 int x = 12; x is 10 Hexadecimal 0x or (zero) along with an x 0-9 and a-f or A-F int x = 0XA; x is 10 Binary or Base(2) Allowed Digits 0 and 1 int i = 10; and now the variable i has value 10. i...

Array List - Collections Framework in Java - DSA

Gist: An array list can store individual objects by following insertion order, here the initial capacity is 10 by default but can be modified as per the requirement, once the array list reaches its load factor then internal all the elements of the current array is copied to a new array with the new capacity and the reference variable will now be referring to this new array list and the old array will be dealt by the garbage collector. Hierarchical order Type of constructors Empty argument constructor or the default constructor is the same as invoking any other object here a new ArrayList is created with a default size of 10. Below is the most commonly used constructor by beginners and others alike. ArrayList array = new ArrayList(); //array has a capacity of 10 The default constructor above will allot only 10 slots but if you want the initial size to be 20 or 1000 you can do so with the following constructor this is ideal w...