Let's learn java programming language with easy steps

Sunday, 15 January 2017

Java Naming Conventions


What is a Naming Convention in Java?

A naming convention in java is a rule to follow as you decide what to name your identifiers (e.g class, variable, method, package, constant, etc...).

Java Naming Conventions

Why use Naming Conventions in Java?

By using standard java naming conventions they make their code easier to read for themselves and for other programmers . They can also give the information about the function of the identifiers for example, whether it is a constant, class , variable, package, interface, etc. Readability of java code is important because it means less time is spent trying to figure out what the code does, leaving more time to fix and modifying it.

Let's take an examples

(1) Class Name Identifier

class name should be a noun and start with uppercase letter and in mixed case with the first letter of each internal world capitalized. Try to keep your class name simple and descriptive .
Use whole words, avoid acronyms and abbreviations(unless the abbreviation  is much more widely used than the long form such as URL and HTML).

For example:

class Customer;
class Car;
class HelloWorld;

(2) Variable Name Identifiers

Variable name should start with lowercase letter. Names should be in mixed case. The names should represent  what the value of the variable represents. Internal world start with capital letter.

For example:

int i;
char c;
int orderNumber;
String firstName;

(3) Package Name Identifiers

Package name should be in lowercase letter. With small projects that have a few packages it's okay to just give them simple but meaningful name.

For example:

package java;
package mycalculator;
package util;

(4) Constant Name Identifiers 

The names of a variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores("_"). e.g MAX_PRIORITY.

For example:

static final int MAX_HIEGHT;
static final int MAX_PRIORITY;

CamelCase in Java Naming Conventions

java  follows camelcase syntax for naming the classes, interfaces, methods and variables.when name is combined two words, second word start with uppercase letter e.g ActionEvent, firstName, fisrtRun(), etc.

Identifiers in java

While defining identifiers we must follow some rules, else its lead to compile time error.
  • Identifiers is the name of the basic programming elements.
  • Class name, variables name, method name, object name are identifiers.

Rules of Identifiers

(1) identifier should only contain
  • Digits(0-9)
  • Alphabets(a-z or A-Z)
  • Special characters(_ and $)

(2) identifiers should not start with a digits
  • A digits can be used from second character onwards.
For example:

2Student is wrong : throw error

Student2 is right : run fine

Student200 is right : run fine

(3) identifiers should not contains special character except '_' and '$'

For example:

_Student  is right : run fine
$Student is right : run fine
Student_John is right : run fine
#Student is wrong : error
*Student is wrong : error
Student*john is wrong : error

(4) identifiers is case sensitive
  • Identifiers is case sensitive e.g There is difference between 'a' and 'A'.

(5) identifiers cannot be used as keyword name

For example:

There is 'int' cannot be  used as variable name.
There is 'extend' cannot be use as method name or variable name.


1 comment:

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email