Naming standard in java

Java Naming Conventions

Java naming conventions are sort of guidelines that application programmers are expected to follow to produce consistent and readable code throughout the application. If teams do not follow these conventions, they may collectively write an application code that is hard to read and difficult to understand.

Java heavily uses Camel Case notations for naming the methods, variables etc. and TitleCase notations for classes and interfaces.

Let’s understand these naming conventions in detail with examples.

Package names must be a group of words starting with all lowercase domain names (e.g. com, org, net, etc). Subsequent parts of the package name may be different according to an organization’s own internal naming conventions.

package com.howtodoinjava.webapp.controller; package com.company.myapplication.web.controller; package com.google.search.common;

In Java, class names generally should be nouns, in title-case with the first letter of each separate word capitalized. e.g.

public class ArrayList <> public class Employee <> public class Record <> public class Identity <>

In Java, interfaces names, generally, should be adjectives. Interfaces should be in the title case with the first letter of each separate word capitalized. In some cases, interfaces can be nouns as well when they present a family of classes e.g. List and Map .

public interface Serializable <> public interface Clonable <> public interface Iterable <> public interface List <>

Methods always should be verbs. They represent action and the method name should clearly state the action they perform. The method name can be single or 2-3 words as needed to clearly represent the action. Words should be in camel case notation.

public Long getId() <> public void remove(Object o) <> public Object update(Object o) <> public Report getReportById(Long id) <> public Report getReportByName(String name) <>

All instance, static and method parameter variable names should be in camel case notation. They should be short and enough to describe their purpose. Temporary variables can be a single character e.g. the counter in the loops.

public Long id; public EmployeeDao employeeDao; private Properties properties; for (int i = 0; i

6. Constant Naming Conventions

Java constants should be all UPPERCASE where words are separated by underscore character (“_”). Make sure to use the final modifier with constant variables.

public final String SECURITY_TOKEN = ". "; public final int INITIAL_SIZE = 16; public final Integer MAX_SIZE = Integer.MAX;

Generic type parameter names should be uppercase single letters. The letter ‘T’ for type is typically recommended. In JDK classes, E is used for collection elements, S is used for service loaders, and K and V are used for map keys and values.

public interface Map <> public interface List extends Collection <> Iterator iterator() <>

Similar to class constants, enumeration names should be all uppercase letters.

Читайте также:  Адаптивный шаблон

Annotation names follow title case notation. They can be adjectives, verbs, or nouns based on the requirements.

public @interface FunctionalInterface <> public @interface Deprecated <> public @interface Documented <> public @Async Documented

In this post, we discussed the naming conventions in Java to be followed for consistent writing of code which makes the code more readable and maintainable.

Naming conventions are probably the first best practice to follow while writing clean code in any programming language.

Источник

Using Java Naming Conventions

Business man sitting infront of computer, backview

Paul Leahy is a computer programmer with over a decade of experience working in the IT industry, as both an in-house and vendor-based developer.

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

Why Use Naming Conventions?

Different Java programmers can have different styles and approaches to the way they program. By using standard Java naming conventions they make their code easier to read for themselves and for other programmers. 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 or modify it.

To illustrate the point it’s worth mentioning that most software companies will have a document that outlines the naming conventions they want their programmers to follow. A new programmer who becomes familiar with those rules will be able to understand code written by a programmer who might have left the company many years beforehand.

Picking a Name for Your Identifier

When choosing a name for an identifier, make sure it’s meaningful. For instance, if your program deals with customer accounts then choose names that make sense for dealing with customers and their accounts (e.g., customerName, accountDetails). Don’t worry about the length of the name. A longer name that sums up the identifier perfectly is preferable to a shorter name that might be quick to type but ambiguous.

A Few Words About Cases

Using the right letter case is the key to following a naming convention:

  • Lowercase is where all the letters in a word are written without any capitalization (e.g., while, if, mypackage).
  • Uppercase is where all the letters in a word are written in capitals. When there are more than two words in the name use underscores to separate them (e.g., MAX_HOURS, FIRST_DAY_OF_WEEK).
  • CamelCase (also known as Upper CamelCase) is where each new word begins with a capital letter (e.g., CamelCase, CustomerAccount, PlayingCard).
  • Mixed case (also known as Lower CamelCase) is the same as CamelCase except the first letter of the name is in lowercase (e.g., hasChildren, customerFirstName, customerLastName).
Читайте также:  Javascript get child element with class

Standard Java Naming Conventions

The below list outlines the standard Java naming conventions for each identifier type:

  • Packages: Names should be in lowercase. With small projects that only have a few packages it’s okay to just give them simple (but meaningful!) names:
package pokeranalyzer package mycalculator

In software companies and large projects where the packages might be imported into other classes, the names will normally be subdivided. Typically this will start with the company domain before being split into layers or features:

package com.mycompany.utilities package org.bobscompany.application.userinterface
class Customer class Account
interface Comparable interface Enumerable
interface IComparable interface IEnumerable
void calculateTax() string getSurname()
string firstName int orderNumber
static final int DEFAULT_WIDTH static final int MAX_HEIGHT

Источник

Naming convention for objects in Java

I had started programming in Java 2 years back. Seniors in our project at that time had advised me to append ‘obj’ to the name of the object which is being created. Ex:

Car objCar = new Car("Ferrari"); 

Here objCar is what I am talking about. But many at Stack Overflow had opposed to it and now I find that this shouldn’t be the way naming of an object should be done. I am clear with naming conventions when collections are used but I am confused when objects of general classes are created. Can anyone shed some light on it?

7 Answers 7

Just call it car . Hungarian notation is not used by Sun/Oracle.

Name your variables in a manner which describes their use. But you don’t need to indicate the type in the variable name. You already specified the type in the variable declaration. IDEs will show you the type of the variable if you mouse-over them so that information is always readily available.

You should use lowercaseStartingCamelCase for variable names and method names, and UppercaseStartingCamelCase for class names.

If you want to read how Sun/Oracle do things, you can read their Code Conventions for the Java Programming Language.

What is the standards followed in naming the objects ? Just the class name with small cased letter in the beginning?

@Ajj Consider naming it something meaningful, like what it does/is used for. That’s the «standard». If you mean grammatical standards, variable names start with a lowercase letter and use camelCase.

Am fine with accessors and mutator methods naming. I am just confused with general classes like ‘Car’, ‘Student’, ‘Machine’, etc..

Name it after what it’s actually being used for, not after the class name. There’s no general rule for that.

Thanks for the link Greg. Had browsed it before but dint help me clarifying my doubt. Anyways, now I understood hungarian way of naming should be completely omitted.

Читайте также:  Убрать все html теги python

Your example is a bit too trivial. In reality you would never name a field of object type «Car» as «car». Likewise you never name an «Integer» type as «integer».

Instead, the use names that tell the reader what the field is used for. Some examples:

private Transport preferredTransportMethod; private int invoiceCounter; 

Prefixing field type is not used commonly is Java. However, class member are sometimes prefixed with lower case «m» to prevent accidental self assignments in setter methods.

private long mTripDurationMillis; 

Name your objects what they are, it makes reading and refactoring easier.

naming conventions for objects in java are just names. For example

The remnants of Hungarian notation have been abandoned mainly because of the added support the IDE’s have provided for navigation.

class name in small letters is Sun/Oracle standard.

when you are working for a company,

depending upon project naming conventions it may vary.

Car car=new Car(); //good to follow 

if It is Singleton pattern,you may give a name like below

 private static final Car singletonCar=new Car(); 

Singletons should not be identified in the field name. The fact that they are singletons should only be visible when the field is initialized. If something else is needed, then it is a signal about something being done wrong. In your example, the wrong thing is that you allow more than one instance of your Car-singleton.

edited.there are many ways to create a singleton it is one of them.This kind of naming convention is maintained just for readability .

The idea of singleton is that you can only initialize one instance of the object. A final field does not prevent you from creating two Car objects. You have to build the restriction into the Car object itself. Initializing singletons is pretty well documented. Please Google it. Once you have acquired a reference to a singleton, there should not be any need to label it as a singleton in the field name. The singleton should be implemented in such a robust manner that the user does not have to know about it after initialization. It should act just like any other object shared by many threads.

just I am again saying there are n number of ways to create a singleton this is one ,of course traditional way,you just go through wikipedia link,discusion about singleton is offtrack here. and moreover when I am giving singletonCar ,it is for all team members,when you are working in a team all team members may not be of same level knowledge and we can not assume that .That is for readability and maintainablity of code.

Источник

Оцените статью