Value of static variable not changed even after initializing the child class in java?
Clash Royale CLAN TAG#URR8PPP
up vote
12
down vote
favorite
class par
static int y = 4;
class checks extends par
static
y = 5;
public class check
public static void main(String args)
System.out.println(checks.y);// here printing 4
As I invoked y
static variable using the subclass.y still static block is not executing and value of y
didn't get updated.
what could be the reason behind it?
As static is shared among all subclasses so the value is supposed to be updated.
where am I getting wrong?
java inheritance jvm static-variables static-block
add a comment |Â
up vote
12
down vote
favorite
class par
static int y = 4;
class checks extends par
static
y = 5;
public class check
public static void main(String args)
System.out.println(checks.y);// here printing 4
As I invoked y
static variable using the subclass.y still static block is not executing and value of y
didn't get updated.
what could be the reason behind it?
As static is shared among all subclasses so the value is supposed to be updated.
where am I getting wrong?
java inheritance jvm static-variables static-block
I know this comment is just noise, but it's worth saying. Excellent Question!
– Roshana Pitigala
15 mins ago
add a comment |Â
up vote
12
down vote
favorite
up vote
12
down vote
favorite
class par
static int y = 4;
class checks extends par
static
y = 5;
public class check
public static void main(String args)
System.out.println(checks.y);// here printing 4
As I invoked y
static variable using the subclass.y still static block is not executing and value of y
didn't get updated.
what could be the reason behind it?
As static is shared among all subclasses so the value is supposed to be updated.
where am I getting wrong?
java inheritance jvm static-variables static-block
class par
static int y = 4;
class checks extends par
static
y = 5;
public class check
public static void main(String args)
System.out.println(checks.y);// here printing 4
As I invoked y
static variable using the subclass.y still static block is not executing and value of y
didn't get updated.
what could be the reason behind it?
As static is shared among all subclasses so the value is supposed to be updated.
where am I getting wrong?
java inheritance jvm static-variables static-block
java inheritance jvm static-variables static-block
edited 19 mins ago


Roshana Pitigala
4,31462345
4,31462345
asked 45 mins ago
rawat
754
754
I know this comment is just noise, but it's worth saying. Excellent Question!
– Roshana Pitigala
15 mins ago
add a comment |Â
I know this comment is just noise, but it's worth saying. Excellent Question!
– Roshana Pitigala
15 mins ago
I know this comment is just noise, but it's worth saying. Excellent Question!
– Roshana Pitigala
15 mins ago
I know this comment is just noise, but it's worth saying. Excellent Question!
– Roshana Pitigala
15 mins ago
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
2
down vote
The field y
does not belong to class checks
.
Even if it's accessed with checks.y
, the compiler surely knows that the only class being used in all of this is par
.
In other words, the class checks
is not being used at runtime.
This is an example of the wrong of accessing static members through subclasses, for which you get a compile-time warning.
add a comment |Â
up vote
1
down vote
From JLS 12.4.1 :
A class or interface type T will be initialized immediately before the
first occurrence of any one of the following:
- T is a class and an instance of T is created.
- T is a class and a static method declared by T is invoked.
- A static field declared by T is assigned.
- A static field declared by T is used and the field is not a constant variable (§4.12.4).
- T is a top level class (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.
Since y is not declared in checks, none of the above criteria is satisfied.
Another way to illustrate this behavior :
class par
static int y = 4;
static
System.out.println("par static constructor");
class checks extends par
static int x = 6;
static
System.out.println("checks static constructor");
y = 5;
public class check
public static void main(String args)
System.out.println(checks.y);
System.out.println(checks.x);
System.out.println(checks.y);
Output
par static constructor
4
checks static constructor
6
5
So after invoking checks.x
that satisfies the second rule, the static constructor gets invoked.
add a comment |Â
up vote
1
down vote
That's because the static
block in the checks
class doesn't get executed. Although you mention the class checks
the JVM doesn't load it at all.
You can confirm this by adding another System.out.println
inside the static block.
class checks extends par
static
System.out.println("Test");
y = 5;
The word Test
will never get printed.
Read the section 12.4.1. When Initialization Occurs of the Java Language Specification to know more.
add a comment |Â
up vote
0
down vote
It seems the Classloader performs some optimization here, and since it determines that 'checks' isn't really used, it skips running its static
block.
If you force checks
to be loaded, e.g., by instantiating an object of that type, you'll indeed see 5
printed.
checks
can be utility class that doesn't need to be instantiate
– user7294900
35 mins ago
add a comment |Â
up vote
0
down vote
As other mention, static block isn't executed because class isn't initialized as answered before,
Notice the class convention names start with an upper case
A clearer example to show Overriding class isn't used:
class Par
static int y = 4;
public static void main(String args)
System.out.println(Checks.y); // here printing 4
System.out.println(new Checks().y); // here printing 5
class Checks extends Par
static
y = 5;
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
The field y
does not belong to class checks
.
Even if it's accessed with checks.y
, the compiler surely knows that the only class being used in all of this is par
.
In other words, the class checks
is not being used at runtime.
This is an example of the wrong of accessing static members through subclasses, for which you get a compile-time warning.
add a comment |Â
up vote
2
down vote
The field y
does not belong to class checks
.
Even if it's accessed with checks.y
, the compiler surely knows that the only class being used in all of this is par
.
In other words, the class checks
is not being used at runtime.
This is an example of the wrong of accessing static members through subclasses, for which you get a compile-time warning.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
The field y
does not belong to class checks
.
Even if it's accessed with checks.y
, the compiler surely knows that the only class being used in all of this is par
.
In other words, the class checks
is not being used at runtime.
This is an example of the wrong of accessing static members through subclasses, for which you get a compile-time warning.
The field y
does not belong to class checks
.
Even if it's accessed with checks.y
, the compiler surely knows that the only class being used in all of this is par
.
In other words, the class checks
is not being used at runtime.
This is an example of the wrong of accessing static members through subclasses, for which you get a compile-time warning.
answered 31 mins ago
ernest_k
16.8k41533
16.8k41533
add a comment |Â
add a comment |Â
up vote
1
down vote
From JLS 12.4.1 :
A class or interface type T will be initialized immediately before the
first occurrence of any one of the following:
- T is a class and an instance of T is created.
- T is a class and a static method declared by T is invoked.
- A static field declared by T is assigned.
- A static field declared by T is used and the field is not a constant variable (§4.12.4).
- T is a top level class (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.
Since y is not declared in checks, none of the above criteria is satisfied.
Another way to illustrate this behavior :
class par
static int y = 4;
static
System.out.println("par static constructor");
class checks extends par
static int x = 6;
static
System.out.println("checks static constructor");
y = 5;
public class check
public static void main(String args)
System.out.println(checks.y);
System.out.println(checks.x);
System.out.println(checks.y);
Output
par static constructor
4
checks static constructor
6
5
So after invoking checks.x
that satisfies the second rule, the static constructor gets invoked.
add a comment |Â
up vote
1
down vote
From JLS 12.4.1 :
A class or interface type T will be initialized immediately before the
first occurrence of any one of the following:
- T is a class and an instance of T is created.
- T is a class and a static method declared by T is invoked.
- A static field declared by T is assigned.
- A static field declared by T is used and the field is not a constant variable (§4.12.4).
- T is a top level class (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.
Since y is not declared in checks, none of the above criteria is satisfied.
Another way to illustrate this behavior :
class par
static int y = 4;
static
System.out.println("par static constructor");
class checks extends par
static int x = 6;
static
System.out.println("checks static constructor");
y = 5;
public class check
public static void main(String args)
System.out.println(checks.y);
System.out.println(checks.x);
System.out.println(checks.y);
Output
par static constructor
4
checks static constructor
6
5
So after invoking checks.x
that satisfies the second rule, the static constructor gets invoked.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
From JLS 12.4.1 :
A class or interface type T will be initialized immediately before the
first occurrence of any one of the following:
- T is a class and an instance of T is created.
- T is a class and a static method declared by T is invoked.
- A static field declared by T is assigned.
- A static field declared by T is used and the field is not a constant variable (§4.12.4).
- T is a top level class (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.
Since y is not declared in checks, none of the above criteria is satisfied.
Another way to illustrate this behavior :
class par
static int y = 4;
static
System.out.println("par static constructor");
class checks extends par
static int x = 6;
static
System.out.println("checks static constructor");
y = 5;
public class check
public static void main(String args)
System.out.println(checks.y);
System.out.println(checks.x);
System.out.println(checks.y);
Output
par static constructor
4
checks static constructor
6
5
So after invoking checks.x
that satisfies the second rule, the static constructor gets invoked.
From JLS 12.4.1 :
A class or interface type T will be initialized immediately before the
first occurrence of any one of the following:
- T is a class and an instance of T is created.
- T is a class and a static method declared by T is invoked.
- A static field declared by T is assigned.
- A static field declared by T is used and the field is not a constant variable (§4.12.4).
- T is a top level class (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.
Since y is not declared in checks, none of the above criteria is satisfied.
Another way to illustrate this behavior :
class par
static int y = 4;
static
System.out.println("par static constructor");
class checks extends par
static int x = 6;
static
System.out.println("checks static constructor");
y = 5;
public class check
public static void main(String args)
System.out.println(checks.y);
System.out.println(checks.x);
System.out.println(checks.y);
Output
par static constructor
4
checks static constructor
6
5
So after invoking checks.x
that satisfies the second rule, the static constructor gets invoked.
answered 23 mins ago
Diadistis
10.5k12746
10.5k12746
add a comment |Â
add a comment |Â
up vote
1
down vote
That's because the static
block in the checks
class doesn't get executed. Although you mention the class checks
the JVM doesn't load it at all.
You can confirm this by adding another System.out.println
inside the static block.
class checks extends par
static
System.out.println("Test");
y = 5;
The word Test
will never get printed.
Read the section 12.4.1. When Initialization Occurs of the Java Language Specification to know more.
add a comment |Â
up vote
1
down vote
That's because the static
block in the checks
class doesn't get executed. Although you mention the class checks
the JVM doesn't load it at all.
You can confirm this by adding another System.out.println
inside the static block.
class checks extends par
static
System.out.println("Test");
y = 5;
The word Test
will never get printed.
Read the section 12.4.1. When Initialization Occurs of the Java Language Specification to know more.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
That's because the static
block in the checks
class doesn't get executed. Although you mention the class checks
the JVM doesn't load it at all.
You can confirm this by adding another System.out.println
inside the static block.
class checks extends par
static
System.out.println("Test");
y = 5;
The word Test
will never get printed.
Read the section 12.4.1. When Initialization Occurs of the Java Language Specification to know more.
That's because the static
block in the checks
class doesn't get executed. Although you mention the class checks
the JVM doesn't load it at all.
You can confirm this by adding another System.out.println
inside the static block.
class checks extends par
static
System.out.println("Test");
y = 5;
The word Test
will never get printed.
Read the section 12.4.1. When Initialization Occurs of the Java Language Specification to know more.
edited 4 mins ago
answered 27 mins ago


Roshana Pitigala
4,31462345
4,31462345
add a comment |Â
add a comment |Â
up vote
0
down vote
It seems the Classloader performs some optimization here, and since it determines that 'checks' isn't really used, it skips running its static
block.
If you force checks
to be loaded, e.g., by instantiating an object of that type, you'll indeed see 5
printed.
checks
can be utility class that doesn't need to be instantiate
– user7294900
35 mins ago
add a comment |Â
up vote
0
down vote
It seems the Classloader performs some optimization here, and since it determines that 'checks' isn't really used, it skips running its static
block.
If you force checks
to be loaded, e.g., by instantiating an object of that type, you'll indeed see 5
printed.
checks
can be utility class that doesn't need to be instantiate
– user7294900
35 mins ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
It seems the Classloader performs some optimization here, and since it determines that 'checks' isn't really used, it skips running its static
block.
If you force checks
to be loaded, e.g., by instantiating an object of that type, you'll indeed see 5
printed.
It seems the Classloader performs some optimization here, and since it determines that 'checks' isn't really used, it skips running its static
block.
If you force checks
to be loaded, e.g., by instantiating an object of that type, you'll indeed see 5
printed.
answered 37 mins ago
Mureinik
173k21122190
173k21122190
checks
can be utility class that doesn't need to be instantiate
– user7294900
35 mins ago
add a comment |Â
checks
can be utility class that doesn't need to be instantiate
– user7294900
35 mins ago
checks
can be utility class that doesn't need to be instantiate– user7294900
35 mins ago
checks
can be utility class that doesn't need to be instantiate– user7294900
35 mins ago
add a comment |Â
up vote
0
down vote
As other mention, static block isn't executed because class isn't initialized as answered before,
Notice the class convention names start with an upper case
A clearer example to show Overriding class isn't used:
class Par
static int y = 4;
public static void main(String args)
System.out.println(Checks.y); // here printing 4
System.out.println(new Checks().y); // here printing 5
class Checks extends Par
static
y = 5;
add a comment |Â
up vote
0
down vote
As other mention, static block isn't executed because class isn't initialized as answered before,
Notice the class convention names start with an upper case
A clearer example to show Overriding class isn't used:
class Par
static int y = 4;
public static void main(String args)
System.out.println(Checks.y); // here printing 4
System.out.println(new Checks().y); // here printing 5
class Checks extends Par
static
y = 5;
add a comment |Â
up vote
0
down vote
up vote
0
down vote
As other mention, static block isn't executed because class isn't initialized as answered before,
Notice the class convention names start with an upper case
A clearer example to show Overriding class isn't used:
class Par
static int y = 4;
public static void main(String args)
System.out.println(Checks.y); // here printing 4
System.out.println(new Checks().y); // here printing 5
class Checks extends Par
static
y = 5;
As other mention, static block isn't executed because class isn't initialized as answered before,
Notice the class convention names start with an upper case
A clearer example to show Overriding class isn't used:
class Par
static int y = 4;
public static void main(String args)
System.out.println(Checks.y); // here printing 4
System.out.println(new Checks().y); // here printing 5
class Checks extends Par
static
y = 5;
edited 9 mins ago
answered 17 mins ago


user7294900
16.2k92953
16.2k92953
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53138984%2fvalue-of-static-variable-not-changed-even-after-initializing-the-child-class-in%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I know this comment is just noise, but it's worth saying. Excellent Question!
– Roshana Pitigala
15 mins ago