Java Tutorial

Variables in java

A variable is an identifier whose value will be changed during execution of the program

Rules for writing variables

  1. First letter must be an alphabet.
  2. The length of the variable should not exceed more than 32 characters.
  3. No special symbols are allowed except underscore.
  4. No keywords should use as variable names.

Types of variables in JAVA:

Whenever we develop any JAVA program that will be developed with respect to class only.

In a class we can use 'n' number of data members and 'n' number of methods.

Generally in JAVA, we can use two types of data members or variables. They are instance/non-static variables and static variables.

INSTANCE/NON-STATIC VARIABLES

  1. An instance variable is one whose memory space is creating each and every time whenever an object is created.
  2. Programmatically instance variable declaration should not be preceded by keyword static.
  3. Data type v1, v2?vn;
  4. Instance variable must be accessed with respect to object name i.e., objname.varname;
  5. Value of instance variable is not sharable.
  6. Instance variable are also known as object level data members since they are dependent on objects.

STATIC VARIABLES

  1. Static variables are whose memory space is creating only once when the class is loaded by class loader subsystem (a part of JVM) in the main memory irrespective of number of objects.
  2. Programmatically static variable declaration must be preceded by keyword static.
  3. Static data type v1, v2?vn;
  4. Static variables must be accessed with respect to class name i.e., classname.varname;
  5. Value of static variable is always recommended for sharable.
  6. Static variable are also known as class level data members since they are dependent on classes.