Is CLASS VARIABLE changed?
I have two classes in same package. I defined a class variable 'no_of_gear' and later changed its value. The changed value
works in the same class but does not work in sub-class in same package.
class ClassVar{
staticint no_of_gear = 5;
publicstaticvoid main(String[] args){
System.out.println(no_of_gear);//Prints 5
ClassVar cv =new ClassVar();
System.out.println(cv.no_of_gear);//Prints 5
cv.no_of_gear = 8;
System.out.println(cv.no_of_gear);//Prints 8
System.out.println(no_of_gear);//Prints 8
}
}
Here is sub-class in same package.
class ClassVarAccextends ClassVar{
publicstaticvoid main(String[] args){
System.out.println(no_of_gear);//Prints 5
ClassVar cv =new ClassVar();
System.out.println(cv.no_of_gear);//Prints 5
}
}
Java Tutorial by SUN reveals that if class variable is changed by one object it is changed for all objects of that class. But, my code does not support the statement. Please, Can you clarify it?
> Java Tutorial by SUN reveals that if class variable
> is changed by one object it is changed for all
> objects of that class. But, my code does not support
> the statement. Please, Can you clarify it?
Your code is rubbish.
Where in your subclass do you change the value of no_of_gear?
> Where in your subclass do you change the value of> no_of_gear?It is changed in its SUPER CLASS.
> > Where in your subclass do you change the value of> > no_of_gear?> > It is changed in its SUPER CLASS.Only if you called the main method of the super class.Do you know the difference between a constructor and a main method?
> > > Where in your subclass do you change the value
> of
> > > no_of_gear?
> >
> > It is changed in its SUPER CLASS.
>
> Only if you called the main method of the super
> class.
>
> Do you know the difference between a constructor and
> a main method?
Yes i do but not to the extent you are pointing to. Can you answer in details?
> > > > Where in your subclass do you change the value
> > of
> > > > no_of_gear?
> > >
> > > It is changed in its SUPER CLASS.
> >
> > Only if you called the main method of the super
> > class.
> >
> > Do you know the difference between a constructor
> and
> > a main method?
>
> Yes i do but not to the extent you are pointing to.
Why can't you just say you don't when you don't?
> Can you answer in details?
Look at your super class
class ClassVar {
static int no_of_gear = 5;
public static void main(String[] args) {
System.out.println(no_of_gear); //Prints 5
ClassVar cv = new ClassVar();
System.out.println(cv.no_of_gear); //Prints 5
cv.no_of_gear = 8;
System.out.println(cv.no_of_gear); //Prints 8
System.out.println(no_of_gear); //Prints 8
}
}
In what method does the value of no_of_gear change?
Now look at the sub-class. Do you see yourself calling this method?
> > Yes i do but not to the extent you are pointing
to.
> Why can't you just say you don't when you don't?
>
> > Can you answer in details?
>
> Look at your super class
>
> class ClassVar {
>
> static int no_of_gear = 5;
>
> public static void main(String[] args) {
>
> System.out.println(no_of_gear); //Prints 5
>
> ClassVar cv = new ClassVar();
> System.out.println(cv.no_of_gear); //Prints 5
>
> cv.no_of_gear = 8;
> System.out.println(cv.no_of_gear); //Prints 8
>
> System.out.println(no_of_gear); //Prints 8
> }
> }
>
> In what method does the value of no_of_gear change?
>
> Now look at the sub-class. Do you see yourself
> calling this method?
OH my dear. It is class variable that can be accessed directly (unlike instance variables) without the need of some method to change its value.
Here i change it. (Remeber: a copy of instance variable is passed to an instance of some class but no copy of class variable is passed to an instance of that class)
cv.no_of_gear = 8;
System.out.println(cv.no_of_gear); //Prints 8
Here it works fine.
System.out.println(no_of_gear); //Prints 8
But, why does not it work in sub-class?
This really seems hopeless. :(
New-Drop@Java-Sea,
Please, please give me the benefit of the doubt here and just assume I might know what I am talking about. That could be helpful.
>
> OH my dear. It is class variable that can be accessed
> directly (unlike instance variables) without the need
> of some method to change its value.
>
Yes. I understand this.
However.
You still need to have code like this
no_of_gear = 8;
Okay. Where is that code?
That code is inside your main method.
Do you see that?
> This really seems hopeless. :(Thankyou very much for your effort. You are great otherwise some people go on posting wrong or un-related answers.
> New-Drop@Java-Sea,
>
> Please, please give me the benefit of the doubt here
> and just assume I might know what I am talking about.
> That could be helpful.
> You still need to have code like this
>
> no_of_gear = 8;
>
> Okay. Where is that code?
>
> That code is inside your main method.
>
> Do you see that?
That really strikes me. Let me work on it and i will respond you after my testing. Thank you again.
> > New-Drop@Java-Sea,
> >
> > Please, please give me the benefit of the doubt
> here
> > and just assume I might know what I am talking
> about.
> > That could be helpful.
>
> > You still need to have code like this
> >
> > no_of_gear = 8;
> >
> > Okay. Where is that code?
> >
> > That code is inside your main method.
> >
> > Do you see that?
>
> That really strikes me.
Okay good.
The issue is that you are changing the value of the static variable from inside your main method. So calling the constructor from any other class will not do you any good. You would need to call the main method of the class.