Variables In Java-Types of variables in java with examples
Variables are defined by giving the name of data types before the variable. In Java, variables must be defined prior to their use. A variable stores values of data types .A variable has a particular size and a value associated with it. In Java, variables can only store values of primitive data types and references to object. Java defines the three types of variables. Each variable can store values of primitive data types or references to objects only.
Local variable(method automatic variable) —> These are the variables declared within a method. These are directly accessed.
Static variable(class variable) —> memory allocated only once. These variables declared using static keyword. These are directly accessed.
Instance variable —> These are the variables declared inside the class. These are similar to local variable. Accessing is done through object
Default values for member variables
If static variables in a class are not explicitly initialized, then when the class is loaded they are initialized to default value.
Instance variables are also initialized to default values when the class is instantiated, unless they are explicitly initialized to some value
A reference variable is initialized to the value null
Local variable are not initialized
Different type varible declaration and printing program
Class Variable{
static int age=25; // static variable
int ID=498; // instance variable
public static void main(String args[]){
String name=”Habiba”; // local variable
System.out.println(name); // Habiba printed
System.out.println(age); // 25 printed
Variable object1=new Variable(); // object creation
System.out.println(object1.ID); // 498printed
}
}
data type in java
Different type variable declaration and printing program in eclipse IDE
Comments
Post a Comment