Skip to content
angelofraietta edited this page Jan 19, 2018 · 8 revisions

If you look at the following link, we can see that there is no difference between the naming of local variable with class variables http://www.oracle.com/technetwork/java/codeconventions-135099.html

Also look at https://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.4

Coding standard

In order to quickly differentiate between class names, class methods, class variables and local variables. Additionally, this will prevent accidental errors caused by scope inaccuracies.

Consider the following fragment where the variable someVariable exists as a class variable, a local variable, and as a parameter in a function

public class MyClassName {
  public Label showVariableLabel;

  private String someVariable = "My Class Variable";

  public void showClassScopeText(ActionEvent actionEvent) {
    showVariableLabel.setText(someVariable);
  }

  void displayLocalScopeInParameter(String someVariable) {
    // even though this next line of code is the same as that in function showClassScopeText,
    // it behaves differently and does not even generate a warning
    showVariableLabel.setText(someVariable);
  }

  public void showLocalScope(ActionEvent actionEvent) {
    // This is bad
    String someVariable = "Local Variable";

    // even though this next line of code is the same as that in function showClassScopeText,
    // it behaves differently and does not even generate a warning
    showVariableLabel.setText(someVariable);
  }

  public void showVariableScope(ActionEvent actionEvent) {
    displayLocalScopeInParameter ("Function Parameter");
  }
}

The line showVariableLabel.setText(someVariable) will behave differently just because the name

Class names and types (e.g. Enum) will be in UpperCamelCase, where the first letter of each word is in upper case

Variables and functions in class scope will be named using lowerCamelCase, where the first letter of variable is lowercase, while each subsequent first letter of next word it upper case.

Constants should be in UPPER_CASE

Variables in local scope,

e.g. Class MyClass { enum EnumType {ENUM_1, ENUM_2};

String myMemberVariableName == “Some Class Value”; 

void someFunction (String my_function_parameter)
{
	String  my_local_variable = “Some local value”;
}

};

IntelliJ makes its function parameters in lowerCamelCase when it generates its own event handlers

void showLocalScope(ActionEvent actionEvent)

Continue to use those and don't worry about changing those because they are very common. However, make sure that use lowercase_parameters when you make your own events. Eg public interface SocketAddressChangedListener{ public void socketChanged(InetAddress old_address, InetAddress new_address); }

Clone this wiki locally