We can pass local variables from one method to another method as parameter.
public class Pro
{
public static void main(String args[])
{
int x=10; // 'x' is local in main
System.out.println("x in main : " + x);
Pro.test(x);
}
static void test(int x) // 'x' is argument(local)
{
System.out.println("x in test : " + x);
}
}
Passing object as parameter:
- Like primitive data types, we can pass objects as parameters.
- We need to collect these parameters into Class type variables in called method.
public class Pro
{
public static void main(String args[])
{
Pro x = new Pro(); // 'x' is local in main
System.out.println("x in main : " + x);
Pro.test(x);
}
static void test(Pro x) // 'x' is argument(local)
{
System.out.println("x in test : " + x);
}
}
Integer static variable default value is ‘0’.
public class Pro
{
static int x;
static
{
System.out.println("x value in block : " + Pro.x);
Pro.x = 10;
}
public static void main(String args[])
{
System.out.println("x value in main : " + Pro.x);
}
}
- Class type variable default value is ‘null’.
- It holds address of that object.
- ‘Pointer variable’ holds address.
public class Pro
{
static Pro x;
static
{
System.out.println("x value in block : " + Pro.x);
Pro.x = new Pro();
}
public static void main(String args[])
{
System.out.println("x value in main : " + Pro.x);
}
}