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

Introduction to Primitives - Java

2. Overview: There are eight primitive data types byte, short, int, long, float, double boolean, char. These eight data types store values as raw instead of Objects these primitives help to save memory to a great extent and simplify other processes as they are directly stored in the stack. Data Type Size(in bits) Minimum Range Maximum Range byte 8 -128 127 short 16 -32768 32767 int 32 -2147483648 2147483647 long 64 -9223372036854775808 9223372036854775807 float 32 -3.4e38 to -1.4e-45 1.4e-45 to 3.4e38 double 64 -1.8e308 to -4.9e-324 4.9e-324 to 1.8e308 boolean 1 - - char 16 space 65535 byte A byte has the capacity of 8 bits or 1 byte and it can store numbers between -2 7 and 2 7 -1 or simply -128 to 127. This is very useful while deali...

Chocolate Feast - Problem Solving - Hacker Rank Solution.

The expectation is to find the total number of choclate one can consume by taking full advantage of the offer, Here there are 3 inputs n which holds the value of initial amount of money for buying choclate, c is the cost price of each candy if paid by cash and m is the exchange rate for the candy. Inputs n Initial cash to buy candy. c Coast of each candy if paid by cas.h m Exchange rate for a new candy in offer. The initial count of choclate will be the cash / coast and the wrappers in hand will be the same value of choclate, and from there we loop through until the wrap count is less than the exchange rate, inside the loop the choclate count will still hold the same fourmula as before but divided with exchange rate. The wrap count is the tricky part... the wrap will be wrap/ exchange rate(the no. choclate) + the remainder of this division(THIS IS VERY IMPORTANT) because for example if the count of wrapper is 3 and the exchange rate is 2 you can only buy 1 c...

Collection Interface - Java Collections Framework - DSA

Most people consider the collection as the root interface of Collections Framework and it is true to a great extent but another part of Collections Framework is Map Interface, we will see that later, Most Common methods which are applicable to all collections are defined in this interface for example add() to add an element, size() to get the size and much more, below is a table of most common methods. Hierarchey of the Collection Interface. The Parent of Collection Interface is Iterator Interface and the Collection is base class for List Interface, Set Interface and Queue Interface, the respective classes which impliments either of the sub classes will also implement the defined methods from the Collection Interface, below are some of the commonly used methods. Defined Methods: Method Description add() This method returns a Boolean value true if it inserts the specified element in this collection....