Invoking and manipulating static variables from different classes
class AQ {
static int t = 6;
int g = 10;
}
class Tr {
public static void main(String[] args) {
//AQ q = new AQ(); // will also work.
AQ q = null;
System.out.println(q.t);
q.t = 5;
System.out.println(q.t);
new Tr().call();
}
AQ a; // ( by default reference variable "a" is assigned null, since you are declaring it at instance(global) level )
void call() {
System.out.println(a.t);
//System.out.println(new AQ().t);
/*
Both the SOP's prints the same value(5) for variable "t" of class - AQ.
*/
}
}
Output:
6
5
5
class Ts {
public static void main(String[] args) {
//AQ q = new AQ(); // will also work.
AQ q = null;
System.out.println(q.t);
}
}
Output:
6
My question is in the call() method of class - Tr output for static variable of class AQ is 5 whereas in class - Ts output for the same static variable of class AQ is 6.
You started the JVM with the Java command( java ) twice.
There is only one instance per JVM, hence the second time you started the JVM, variable "t" will have its original value(6) that has been assigned to it in its own class AQ.
So when you first executed class - Tr and modified the static variable "t" of class - AQ with its reference variable "q" and assigned a new value, all these happened with in the same JVM and single instance of class - AQ. Thats why you got the modified value(5) being printed out in the method - call().
Whereas with class - Ts, you started the JVM seperately using the "java" command.
One more important point to note here is we are using the reference variable "q" to invoke the static variable of class AQ and the reference variable is assigned with "null". Did you ever think why we didn't end up with a NullPointerException. Thats because we are invoking the static variable and NOT instance variable.
instance variable - belongs to the object and
static variable - belongs to the class.
If you invoke a instance variable using a reference variable thats assigned with "null" then you will get
java.lang.NullPointerException.
static int t = 6;
int g = 10;
}
class Tr {
public static void main(String[] args) {
//AQ q = new AQ(); // will also work.
AQ q = null;
System.out.println(q.t);
q.t = 5;
System.out.println(q.t);
new Tr().call();
}
AQ a; // ( by default reference variable "a" is assigned null, since you are declaring it at instance(global) level )
void call() {
System.out.println(a.t);
//System.out.println(new AQ().t);
/*
Both the SOP's prints the same value(5) for variable "t" of class - AQ.
*/
}
}
Output:
6
5
5
class Ts {
public static void main(String[] args) {
//AQ q = new AQ(); // will also work.
AQ q = null;
System.out.println(q.t);
}
}
Output:
6
My question is in the call() method of class - Tr output for static variable of class AQ is 5 whereas in class - Ts output for the same static variable of class AQ is 6.
You started the JVM with the Java command( java
There is only one instance per JVM, hence the second time you started the JVM, variable "t" will have its original value(6) that has been assigned to it in its own class AQ.
So when you first executed class - Tr and modified the static variable "t" of class - AQ with its reference variable "q" and assigned a new value, all these happened with in the same JVM and single instance of class - AQ. Thats why you got the modified value(5) being printed out in the method - call().
Whereas with class - Ts, you started the JVM seperately using the "java
One more important point to note here is we are using the reference variable "q" to invoke the static variable of class AQ and the reference variable is assigned with "null". Did you ever think why we didn't end up with a NullPointerException. Thats because we are invoking the static variable and NOT instance variable.
instance variable - belongs to the object and
static variable - belongs to the class.
If you invoke a instance variable using a reference variable thats assigned with "null" then you will get
java.lang.NullPointerException.
0 Comments:
Post a Comment
<< Home