Let’s see what is local and instance variables in Java.

State of the class can be defined as Variables. In Java, we use variables in different places as,

  • object’s state ( instance variables)
  • local variables ( variables declared within a method)
  • arguments ( value sent to method by the calling code)
  • return type ( value sent back to caller of the method)

How to declare Variables?

When declaring our so called state which is “variable”, you must declare the type of the variable. Variables come in two types as primitive and reference variables.

Primitive holds fundamental values. Reference variables are those references to objects.

Refer primitive data types, size and its limits in below table,

TypeSizeLimit
byte8 bit-128 to 127 (inclusive)
short16 bit-32,768 to 32,767
int32 bit-2 31  to 2 31 -1
long64 bit-2 63 to 2 63 -1
float32 bitSingle precision floating value
double64 bitDouble precision floating value
char16 bit'\u0000’ to '\uffff'
booleanNot definedTrue and false

Regardless of the type, you must follow two rules while declaring variables,

  1. variables must have a type
  2. variables must have a name

Example for variable declaration,

public class SpeedTest {
  int gear;       
  int speed;      
}

In above example, int represents type of variable and gear, speed is name of variable.

 

In Java, primitives comes in different sizes and those sizes have names. While declaring variable in Java, you must declare it with specific type. For example, consider primitives as battery size displayed. For each usage, we use different size of battery. Likewise, while using variable choose which type will be suitable for our variable usage and give a relevant name for variable. Like tell JVM you need an int with value as 2000 and give it a name capacityOfbattery.

 

Example on various type of variable declaration

 int height = 20;                  // declare an int named height and assigning value 20
 char startingLetter = 'j';        // declare a char named startingLetter and assigning value j
 double depth = 300.00;            // declare a double named depth and assigning value 300.00
 boolean isSweet = true;           // declare a boolean named isSweet and assigning value true
	long length = 5002928;            // declare a long named length and assigning value 5002928

Rules to name a variable:

  1. It must begin with a letter, underscore (_), or dollar sign ($). You can’t begin a name with a number.
  2. After the first character, you can use numbers also. Just don’t start it with a number.
  3. It can be anything you like relevant to purpose of variable, but shouldn’t be one of Java’s reserved words.

Instance Variables (Object’s reference variable)

You should be clear on Object’s reference variables and its not object’s variable. There is no such thing as Object’s variable. There is only an object reference variable. 

Object reference variable  is a way to access variables in object using dot(.) operator.

Example for object reference variable:

public class AddressBook {

  String firstName;
  String lastName;
  String phoneNumber;
  
  public void showContactAsString() {
    System.out.println("Name is "+firstName+" "+lastName+" with phone number "+phoneNumber);
  }
}
public class TestAddressBook {
  public static void main(String[] args) {
    AddressBook contact1 = new AddressBook();
    contact1.firstName = "Karl";
    contact1.lastName = "Marx";
    contact1.phoneNumber = "91-44444-55555";
    
    AddressBook contact2 = new AddressBook();
    contact2.firstName = "Tim";
    contact2.lastName = "Benz";
    contact2.phoneNumber = "91-22222-77777";
    
    contact1.showContactAsString();
    contact2.showContactAsString();
  }
}

In above example, we are accessing variable of class AddressBook using reference object contact1 and contact2 using dot(.) operator.

Creating a new object and referencing as contact1 and contact2 that is we are linking the object and its reference,

AddressBook contact1 = new AddressBook();

AddressBook contact2 = new AddressBook();

contact1.firstName = "Karl";

contact1.firstName = "Tim";

accessing firstName variable of  contact1 and contact2 object which is of type AddressBook and assigning a value as “Karl” & “Tim”.

We can create multiple objects from same class as contact1 and contact2 created in above example.

Conclusion

Therefore we saw what is variables, primitive types, how to declare variables and what is instance variables with example in this articles.

Please follow and like us:
error