• Coding Standards(CS) and Naming conventions(NC) are suggestions given by sun(Oracle).
  • CS and NC help developers to develop projects with more readability and understandability

Why Coding Standards? 

  • A program is written once , but read many times
    1. During debugging
    2. When adding to the program
    3. When updating the program
    4. When trying to understand the program
  • Anything that makes a program readable and understandable saves lots of time , even in the shot run.

Naming Conventions: 

1.Naming a class:

  • Class name should be "noun", because it represents things
  • Class name should be in title case , means Every word first letter should be capital letter.
  • This is also known as UpperCamelCase.
  • Choose descriptive and simple names.
  • Try to avoid acronyms and abbreviations.


  1. package com.instanceofjava
  2. public JavaDemo{
  3.  
  4.  }

2.Naming a Variable:

  • Variable name also should be noun, because it represents values.
  • Variable name should be start with small letter. 
  • Use lowerCamelCase.
  • Example: firstName, lastName, age.
  • For final variable all its letters should be capital and words must be connected with "_".
  • Example:  MAX_COUNT, MIN_COUNT.

  1. package com.instanceofjava
  2. public JavaDemo{
  3.  
  4.  String topicName;
  5.  String post;
  6.  int postNumber;
  7.  final static int MIN_WORDS=300;

  8.  }

3.Naming Method:

  • Method name should be verb because it represents action.
  • Use lowerCamelCase for naming methods
  • Example: setFirstName(), getFirstName(), getLastName().

  1. package com.instanceofjava
  2. public JavaDemo{
  3.  
  4.  String topicName;
  5.   
  6. public void setTopicName(String topicName) {
  7. this.topicName=topicName;
  8.  }
  9.  
  10. public void getTopicName() {
  11. return topicName;
  12.  }
  13.  
  14.  }

4.Naming a package:

  • All letters should be small 
  • like in java io, util, applet
  • Example com.instanceofjava

  1. package com.instanceofjava
  2. public JavaDemo{
  3.  }
  • If you do not follow all above naming conventions compiler wont any Error or exception.
  • But naming conventions are  useful in project and need to follow.
  • But we need to follow some identifier rules as follows.

Identifiers:

  • Identifier is the name of the basic programming elements.
  • class names, methods names , object names and variables names are identifiers
  • If we define a basic program without a name , compiler throws compile time error.
  • Compilation Error : <identifier> expected.
  • if we take a class without name then compile time error will come : Syntax error on token "class", Identifier expected after this token.

  1. package com.instanceofjava
  2. public {   //  Syntax error on token "class", Identifier expected after this token.
  3.  }

Rules in defining identifier:

  • While defining identifier we must follow bellow rules, else it leads to compile time error.


1.Identifier should only contain :

  1. Alphabets(a -z and A-Z)
  2. Digits (0-9)
  3. Special Characters ( _ and $)

2.Identifier should not start with a digit:

  • A digit can be used from second character onwards.
  • Example : 
  • 1strank : throws error
  • No1Rank : works fine

3.Identifier should not contain special characters except "-" and "$":

  • Example : 
  • first_name : works fine
  • first#name : throws error

4.Identifier is case sensitive :

  • identifier is case sensitive
  • For example a and A are different a!=A.

5.keyword cannot be used as user defined identifier :

  • we can not use keywords as our identifiers
  • For example int class; // Syntax error on token "class", invalid VariableDeclarator.

Note:

  • we can use predefined class names as identifiers
  • example : int String;
  • There is no limit in identifier length.

Java comments:

  • A description about a basic programming element is called comment.
  • Comments are meant for developer, to understand purpose.

Types of comments:

  • Java supports 3 types of comments
  1. Single line comments  - //
  2. Multiline comments  - /* */
  3. Document comment - /** */

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu