Advertisement
//Accessing a static variable/static method within the scope of its class.
class A
{
static int i=10; //Static variable of class A is initialized to 10
public static void display() //Static method of class A
{
System.out.println("A");
}
public static void main(String... ar)
{
System.out.println(i); //Accessing a static variable directly
System.out.println(A.i); //Accessing a static variable by using its class name and a dot operator.
display(); //Accessing a static method directly
A.display(); //Accessing a static method by using its class name and a dot operator
}
}
10
10
A
A
Advertisement
//Incorrect direct access of a static variable and a static method outside the scope of its class.
class A
{
static int i=10; //Static variable of clsss A is initialized to 10
static void display() //Static method of class A
{
System.out.println("A");
}
}
class B
{
public static void main(String... ar)
{
System.out.println(i); //Accessing a static variable directly, outside the scope of its class A.
display(); //Accessing a static method directly, outside the scope of its class A.
}
}
A.java:16: error: cannot find symbol
System.out.println(i);
^
symbol: variable i
location: class B
A42.java:17: error: cannot find symbol
display();
^
symbol: method display()
location: class B
2 errors
//Accessing static variable, static method outside the scope of its class.
class A
{
static int i=10; //Static variable of class A is initialized to 10
public static void display()
{
System.out.println("A");
}
}
class B
{
public static void main(String... ar)
{
//Accessing the static variable outside its class, using its class name and a dot operator.
System.out.println(A.i);
//Accessing the static method outside its class, using its class name and a dot operator.
A.display();
}
}
10
A
This program is the same as the last one but, in this code, we have accessed a static variable, i, and static method, display(),
outside the scope of its class A, by using its class name and a dot operator, hence, no compile error is thrown.
//Java - Cannot directly access an instance variable from within a static method
class A
{
int i=10; //Non-static i.e. instance variable of class A is initialized to 10
public static void display() //Static method of class A
{
System.out.println(i);
}
public static void main(String... ar)
{
display(); //Directly accessing a static method in its class is fine
}
}
A10.java:7: error: non-static variable i cannot be referenced from a static context
System.out.println(i);
^
1 error
//Java - Indirect access of an instance variable from within a static method
class A
{
int i=10;
static void display(int a)
{
System.out.println(a);
}
public void method2()
{
display(i);
}
public static void main(String... ar)
{
A ob = new A();
ob.method2();
}
}
10
//Java - Default value of a static variable
class A
{
static int i;
static float f;
static String s;
public static void main(String... ar)
{
System.out.println(i);
System.out.println(f);
System.out.println(s);
}
}
0
0.0
null
Static Type | Default Value |
---|---|
int, short, byte, long | 0 |
float, double | 0.0 |
boolean | false |
Object | null |
//Java - A single copy of static variable is shared by all the objects of the class
class A
{
static int value=10; //Static variable of class A
public void setValue(int v)
{
value=v;
}
public int getValue()
{
return value;
}
public static void main(String... ar)
{
A ob1= new A();
ob1.setValue(20); //Setting the value of static variable using ob1
A ob2= new A();
ob2.setValue(30); //Setting the value of static variable using ob2
System.out.println(ob1.getValue()); //Accessing value of static using ob1
System.out.println(ob2.getValue()); //Accessing value of static using ob2
}
}
30
30
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement