local variables java code example
Example 1: instance variable java
A variable declared inside the class is called instance variable. Value of
instance variable are instance specific.
public class InstanceVariableDemo
{
int c;
public void subtract()
{
int x = 100;
int y = 50;
c = x - y;
System.out.println("Subtraction: " + c);
}
public void multiply()
{
int m = 10;
int n = 5;
c = m * n;
System.out.println("Multiplication: " + c);
}
public static void main(String[] args)
{
InstanceVariableDemo obj = new InstanceVariableDemo();
obj.subtract();
obj.multiply();
}
}
Example 2: Variables in java
public class StaticVariableExample
{
static int a = 0;
public void add()
{
a++;
}
public static void main(String[] args)
{
StaticVariableExample obj1 = new StaticVariableExample();
StaticVariableExample obj2 = new StaticVariableExample();
obj1.add();
obj2.add();
System.out.println("Object 1 value is: " + a);
System.out.println("Object 2 value is: " + a);
}
}
Example 3: Variables in java
public class LocalVariableExample
{
public void employeeDetails()
{
int empAge = 22;
String name = "Sachin";
}
public static void main(String[] args)
{
LocalVariableExample obj = new LocalVariableExample();
System.out.println("Employee name is : " + name + ", and age : " + empAge);
}
}
Example 4: java variable declared
java decleartion
int a, b, c;
int a = 10, b = 10;
byte B = 22;
double pi = 3.14159;
char a = 'a';