Naming Rules and Conventions

Rules (must follow)

  • Names can contain letters, digits, _ and $.
  • Names cannot start with a digit: 2ndPlace is invalid.
  • Names are case-sensitive: total and Total are different variables.
  • Reserved words (int, class, new, public...) cannot be used as names.
  • No spaces: first name is invalid.

Conventions (should follow)

KindStyleExamples
variables, methodscamelCasefirstName, totalAmount, calculateTax()
classesPascalCaseStudent, OrderService
constantsUPPER_SNAKE_CASEMAX_USERS, GST_RATE
packageslowercasecom.careeroza.orders

Tip: Choose meaningful names. int d = 7; says nothing; int deliveryDays = 7; explains itself.

Practice

Exercise: Which of these names are valid? _count, 2fast, $price, my-name, class, userAge

Show answer
Valid:   _count, $price, userAge
Invalid: 2fast   (starts with a digit)
         my-name (hyphen not allowed)
         class   (reserved keyword)