简体   繁体   中英

Variable naming conventions in Java

In PHP, we (at least the good programmers) always start general variable names with a lower-case letter, but class variables/objects with an upper-case letter to distinguish them. In the same way we start general file names with a lower case letter, but files containing Classes with an upper case letter.

Eg:

<?php
    $number = 123;
    $string = "a string";
    $colors_array = array('red', 'blue', 'red');
    $Cat = New Cat();
?>

Are the conventions the same in Java, ie, objects starting with upper-case, but the rest with lower case, or does everything start with lower case as I've read in other places?

You can find the naming in the Java Code Conventions .

A quick summary:

  • For classes, use UpperCamelCase .
  • For class members and local variables use lowerCamelCase
  • For packages, use reverse URI, eg org.acme.project.subsystem
  • For constants, use ALL_CAPS .

Generally, all variables will start with lower case:

int count = 32;
double conversionFactor = 1.5d;

Some people like to put static constants in all case:

public static final double KILOGRAM_TO_POUND = 2.20462262;

Things get more annoying when you deal with acronyms, and there is no real standard on whether you should use:

HTMLHandler myHtmlHandler; 

or

HTMLHandler myHTMLHandler.

Now, either way, note that the class names (Object, String, HTMLHandler) always start with a capital letter, but individual object variables start lowercase.

  • variablesAndMethodsLikeThis
  • ClassesLikeThis
  • CONSTANTS_LIKE_THIS

The convention is that class names start with Upper Case letter. Variable names are camelCase. Even if the variable references an object it still starts with lower case.

This page should help.

What do you think about using camelCase for instance methods and PascalCase for static? I think it could be very helpful, because there wouldn't be any issues while verifying which method is static. It's my proposal

The conventions really depend on the individual place you're coding for.

In general, from what I've seen classes are CamelCased (with upper case first), methods start with lower case, and variables I've seen all over the place (some CamelCased, some camelCase with first letter lower (EDIT: as mentioned above, this is the norm), some even with hungarian notation). It depends on your style and the style that the project you're working on has adopted.

Some people (who are not me) like to differentiate Method variables from Instance variables by prefixing Instance variables with "this." This also fixes the problem that occurs when assigning a parameter to an Instance variable of the same name:

public ConstructorMethod(MyVar variable) {
    this.variable=variable;
}

But then some people feel you should always use that pattern--but I'm not crazy about it -- I think it's overkill if you keep your methods and classes small.

Also, some people use a naming pattern for parameters. This (again) comes in handy when you are assigning from a constructor to an Instance variable:

public ConstructorMethod(MyVar pVariable) {
    variable=pVariable;
}

Usually the pattern is pVariable or _variable . I occasionally use this because I find it more readable than this, but it has the disadvantage of making your Javadocs less readable.

In any case, I don't really like the idea of ALWAYS using any of these patterns, they are great to know but if you really need help differentiating them throughout your code, tell Eclipse to show them in different colors.

There are some rules for naming convention of variable in java as

  1. Java variable names are case sensitive. The variable name salary is not the same as Salary or SALARY.
  2. Java Variable names must begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
  3. After the first character, identifiers can have any combination of characters.
  4. A reserved keyword cannot be used as variable.

So All instance, static and method parameter variable names should be in camel case notation. Like userName , salary etc. Constant Name as

pulic Static final String USER_NUMBER = 10  

Reference - Java Variable naming conventions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM