how to modify parent class variable with the child class and use in another child class in java -
i have class test has variable count 0. test class extended classes , b. set count in 50. want access count in b, should return value 50, i'm getting 0 in b. i'm new java , dont know how works :( can 1 me how implement this? :(
public class extend { public int count =0; } public class extends extend { this.count = 50; } public class b extends extend { system.out.println ( " count " + count); }
each instance of extend
(including instances of class extends it) gets own copy of count
. values not shared between instances.
you can change b
extend class a
instead of class extend
. instance of b
see whatever initialization done when a
constructor executed.
if want same count
shared instances of class or subclasses, need make field static
:
public class extend { public static int count = 0; }
in subclasses, refer field name without this.
qualifier.
note code posted not legal java. first 2 classes should public class
, not public class
or public class
. also, cannot have code this.count = 50;
outside method or initializer block. i'm assuming not real code.
Comments
Post a Comment