KEMBAR78
Java Variable Types | PDF
In Java, all variables must be declared before they can be used.
Few examples
Instance variables
Technically speaking, objects store their individual states in
“non-static fields”, that is, fields declared without the static keyword.
Non-static fields are also known as instance variables because
their values are unique to each instance of a class
(to each object, in other words);
the currentSpeed of one bicycle is independent
fromthe currentSpeed of another.
Instance variables are declared in a class,
but outside a method, constructor or any block.
Instance variables are created when an object is created
and destroyed when the object is destroyed.
Instance variables hold object's state, available to all methods,
constructors or blocks of a class, and accessible by using references or
objects out the class.
Instance variables have default values
● numbers : 0
● Booleans : false
● Object references : null
Values can be assigned during the declaration
or within the constructor.
Identify Instance variables
Class Variables (Static Fields)
A class variable is any field declared with the static modifier;
there is exactly one copy of this variable in existence,
regardless of how many times the class has been instantiated.
Example :
A field defining the number of gears for a particular kind of bicycle could
be marked as static since conceptually the same number of gears will
apply to all instances.
static int numGears =6
Local variables
Similar to how an object stores its state in fields, a method will often store
its temporary state in local variables.
The syntax for declaring a local variable is similar to declaring a field
There is no special keyword designating a variable as local;
that determination comes entirely from the location in which the
variable is declared
— which is between the opening and closing braces of a method.
Access modifiers cannot be used for local variables.
Local variables are visible only
within the declared method, constructor or block.
There is no default value for local variables so local variables should be
declared and an initial value should be assigned before the first use.
Identify Local variable
How we can fix this error?
Error: variable mmight not have been initialized
m++;
1 error
Parameters
args variable is the parameter to main method.
public static void main ( String[ ] args )
Parameters are always classified as “variables”not “fields”.
This applies to parameter-accepting constructs as well.

Java Variable Types

  • 1.
    In Java, allvariables must be declared before they can be used.
  • 2.
  • 3.
    Instance variables Technically speaking,objects store their individual states in “non-static fields”, that is, fields declared without the static keyword.
  • 4.
    Non-static fields arealso known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent fromthe currentSpeed of another.
  • 5.
    Instance variables aredeclared in a class, but outside a method, constructor or any block.
  • 6.
    Instance variables arecreated when an object is created and destroyed when the object is destroyed.
  • 7.
    Instance variables holdobject's state, available to all methods, constructors or blocks of a class, and accessible by using references or objects out the class.
  • 8.
    Instance variables havedefault values ● numbers : 0 ● Booleans : false ● Object references : null Values can be assigned during the declaration or within the constructor.
  • 9.
  • 10.
    Class Variables (StaticFields) A class variable is any field declared with the static modifier;
  • 11.
    there is exactlyone copy of this variable in existence, regardless of how many times the class has been instantiated.
  • 12.
    Example : A fielddefining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. static int numGears =6
  • 13.
    Local variables Similar tohow an object stores its state in fields, a method will often store its temporary state in local variables.
  • 14.
    The syntax fordeclaring a local variable is similar to declaring a field
  • 15.
    There is nospecial keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method.
  • 16.
    Access modifiers cannotbe used for local variables.
  • 17.
    Local variables arevisible only within the declared method, constructor or block.
  • 18.
    There is nodefault value for local variables so local variables should be declared and an initial value should be assigned before the first use.
  • 19.
  • 20.
    How we canfix this error? Error: variable mmight not have been initialized m++; 1 error
  • 21.
    Parameters args variable isthe parameter to main method. public static void main ( String[ ] args )
  • 22.
    Parameters are alwaysclassified as “variables”not “fields”. This applies to parameter-accepting constructs as well.