Identifiers and Variables (Part 2)

1. Java Identifiers

All Java components require names. Names used for classes, variables, and methods are called identifiers.

In Java, there are several points to remember about identifiers. They are as follows −

  • ▪ All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).

  • ▪  After the first character, identifiers can have any combination of characters.

  • ▪  A key word cannot be used as an identifier.

  • ▪  Most importantly, identifiers are case sensitive.

  • Examples of legal identifiers: age, $id, _value, __1_value.

  • Examples of illegal identifiers: 789abc, -id.


2. Java Variables

    Variables are nothing but a container where we can hold value of that data type i.e int can hold any number , String can hold any character.

TYPES:-


  • ▪  Local Variables
  •  Local variables are declared in methods, constructors, or blocks. Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.

  • ▪  Class Variables (Static Variables)
  •  Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. There would only be one copy of each class variable per class, regardless of how many objects are created from it.

  • ▪  Instance Variables (Non-static Variables)
  • Instance variables are non-static variables and are declared in a class outside any method , constructor or block




< Previous Next >