• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Both Variables are Same?  RSS feed

 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why You.i and Me.i Pointing to the same variable I.
I need full Explanation.....
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:
Why You.i and Me.i Pointing to the same variable I.


Because there's only one i variable declared. The child class inherits it, but it's still just one variable.
 
Sccot Smith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the subclass named Me inherited the parent class named You and won the variables i
But you should notice that the variable is saved in Heap Memory in JVM.
In Stack Memory,You and Me is point to the same piece of memory in Heap Memory.
when invoking you.i and me.i,in fact, it is operating the variable i in Heap Memory in JVM
So the variable i in class you and class me will be also changed when invoking it by you.i and Me.i
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sccot Smith wrote:Because the subclass named Me inherited the parent class named You and won the variables i
But you should notice that the variable is saved in Heap Memory in JVM.
In Stack Memory,You and Me is point to the same piece of memory in Heap Memory.


Please don't post meaningless responses when you don't understand the principles at hand.

Thanks!
 
Sccot Smith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Sccot Smith wrote:Because the subclass named Me inherited the parent class named You and won the variables i
But you should notice that the variable is saved in Heap Memory in JVM.
In Stack Memory,You and Me is point to the same piece of memory in Heap Memory.


Please don't post meaningless responses when you don't understand the principles at hand.

Thanks!

Everyone is entitled to his own opinion,you are not god,you have no right to tell me how to do。
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sccot Smith wrote:
Jeff Verdegan wrote:
Sccot Smith wrote:Because the subclass named Me inherited the parent class named You and won the variables i
But you should notice that the variable is saved in Heap Memory in JVM.
In Stack Memory,You and Me is point to the same piece of memory in Heap Memory.


Please don't post meaningless responses when you don't understand the principles at hand.

Thanks!

Everyone is entitled to his own opinion,you are not god,you have no right to tell me how to do。


I am not trying to deny your opinion, nor am I trying to play god. I am simply asking you not to post views that you are unsure or only guessing about, out of respect for the people whose questions you are trying to answer.

If you thing your response is accurate, please provide support for your position.
 
Sccot Smith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Sccot Smith wrote:
Jeff Verdegan wrote:
Sccot Smith wrote:Because the subclass named Me inherited the parent class named You and won the variables i
But you should notice that the variable is saved in Heap Memory in JVM.
In Stack Memory,You and Me is point to the same piece of memory in Heap Memory.


Please don't post meaningless responses when you don't understand the principles at hand.

Thanks!

Everyone is entitled to his own opinion,you are not god,you have no right to tell me how to do。


I am not trying to deny your opinion, nor am I trying to play god. I am simply asking you not to post views that you are unsure or only guessing about, out of respect for the people whose questions you are trying to answer.

If you thing your response is accurate, please provide support for your position.

Ok,sorry.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:

Because there's only one i variable declared. The child class inherits it, but it's still just one variable.


if the child class inherits somthing .Don't you think ,child class should have it's own copy Sir.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:
Jeff Verdegan wrote:

Because there's only one i variable declared. The child class inherits it, but it's still just one variable.


if the child class inherits somthing .Don't you think ,child class should have it's own copy Sir.


No, that's not what inheritance means. And your own observations show that.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Nikhil Sagar wrote:
Jeff Verdegan wrote:

Because there's only one i variable declared. The child class inherits it, but it's still just one variable.


if the child class inherits somthing .Don't you think ,child class should have it's own copy Sir.


No, that's not what inheritance means. And your own observations show that.


Sir, You mean In Inheritance Child Class Does not have its own copy of Variables ??
Right??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:
Jeff Verdegan wrote:
Nikhil Sagar wrote:
Jeff Verdegan wrote:

Because there's only one i variable declared. The child class inherits it, but it's still just one variable.


if the child class inherits somthing .Don't you think ,child class should have it's own copy Sir.


No, that's not what inheritance means. And your own observations show that.


Sir, You mean In Inheritance Child Class Does not have its own copy of Variables ??
Right??


I thought that would be obvious by now, but, yes, that's correct.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Nikhil Sagar wrote:
Jeff Verdegan wrote:
Nikhil Sagar wrote:
Jeff Verdegan wrote:

Because there's only one i variable declared. The child class inherits it, but it's still just one variable.


if the child class inherits somthing .Don't you think ,child class should have it's own copy Sir.


No, that's not what inheritance means. And your own observations show that.


Sir, You mean In Inheritance Child Class Does not have its own copy of Variables ??
Right??


I thought that would be obvious by now, but, yes, that's correct.


Alright ,Let me think Sir then i will reply just in 10 minutes.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:
Jeff Verdegan wrote:
Nikhil Sagar wrote:
Sir, You mean In Inheritance Child Class Does not have its own copy of Variables ??
Right??


I thought that would be obvious by now, but, yes, that's correct.


Alright ,Let me think Sir then i will reply just in 10 minutes.



Not sure why this seems to be such a common misconception -- a more common misconception, IMO, is thinking that child classes which inherit methods, actually get a copy of the method code. It obviously doesn't, but this misconception keeps coming up.

Henry
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:
Jeff Verdegan wrote:
Nikhil Sagar wrote:
Jeff Verdegan wrote:
Nikhil Sagar wrote:
Jeff Verdegan wrote:

Because there's only one i variable declared. The child class inherits it, but it's still just one variable.


if the child class inherits somthing .Don't you think ,child class should have it's own copy Sir.


No, that's not what inheritance means. And your own observations show that.


Sir, You mean In Inheritance Child Class Does not have its own copy of Variables ??
Right??


I thought that would be obvious by now, but, yes, that's correct.


Alright ,Let me think Sir then i will reply just in 10 minutes.


I don't THiNK so......
Have a look at this then tell me CHILD CLASS DOES NOT HAVE ITS OWN COPY OF VARIABLES ,Sir.
I THINK AT run time the object of child class has its own copy of variables of both classes But We know that object always has the copy of those instance variables who are present in its class So, its a very confusing situation here Because You said that child class does not have copy of the variables of its parent class or the all parent classes into the hierarchy over itself. So, According to me Your Point is also True but only in the case of STATIC variables, Because there is no second copy of static variable available to use AND this is the first reason.Second is we call Static variables as class variables because they belongs to only that particular class only neither to any object nor to any other class.
Correct me if i am wrong.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote=Nikhil Sagar


I don't THiNK so......
Have a look at this ..............

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:I don't THiNK so......
Have a look at this then tell me CHILD CLASS DOES NOT HAVE ITS OWN COPY OF VARIABLES ,Sir.
I THINK AT run time the object of child class has its own copy of variables of both classes But We know that object always has the copy of those instance variables who are present in its class So, its a very confusing situation here Because You said that child class does not have copy of the variables of its parent class or the all parent classes into the hierarchy over itself. So, According to me Your Point is also True but only in the case of STATIC variables, Because there is no second copy of static variable available to use AND this is the first reason.Second is we call Static variables as class variables because they belongs to only that particular class only neither to any object nor to any other class.
Correct me if i am wrong.


Instance variables, defined by the superclass are present in the subclass, because the subclass IS-A the superclass -- it is present because it needs to be, not because the subclass has a copy of it in its definition.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:

I don't THiNK so......
Have a look at this ..............



Modify your subclass to print out "i" and "super.i". You will see that they map to the same instance variable. If you define the subclass to have an "i" variable -- then there will be two different variables. All your "proof" does is prove objects don't share instance variables -- it doesn't prove that the subclass has its own copy of the variable (in its definition that is).

Henry
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if variable i only present in the child class like my first code then why not this code is behaving just like the first one, Sir.
I mean why not both i pointing to the same variable i. If you are going to answer me something like "Because bothe belongs to different objects" then my ques will be like "if child class does not have its own copy and variable i is only present in the child class (but not have any copy) then all object of child class should have same values as it has into the parent class." i mean if i change the value into the parent class then this should also change into the all objects of the child class.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:
I don't THiNK so......
Have a look at this then tell me CHILD CLASS DOES NOT HAVE ITS OWN COPY OF VARIABLES ,Sir.


I don't need to look at it. I already know that there's only one copy. And the code in your first post is evidence of that.

I THINK AT run time the object of child class has its own copy of variables of both classes


It doesn't. You've already seen code that shows that it doesn't. If you're going to claim it does, you'll need to cite the relevant sections of the JLS.

But We know that object always has the copy of those instance variables who are present in its class


That is a completely different situation. Each instance having its own copy of member variables has nothing to do with child and parent having a single copy.



After that code runs, there are the following variables:
1 x (Parent class)
4 y (one for each instance
1 z (Child class)
2 w (One for each Child instance)

So, its a very confusing situation here Because You said that child class does not have copy of the variables of its parent class or the all parent classes into the hierarchy over itself.


Yes, I said that because it's true.

So, According to me Your Point is also True but only in the case of STATIC variables,


No, it's true for non-statics as well. See the above code. Each instance gets its own copy of non-static variables, but if I create an instance of Child, there's not one non-static for the parent and one non-static for the child, just like with statics.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:if variable i only present in the child class like my first code then why not this code is behaving just like the first one, Sir.
I mean why not both i pointing to the same variable i. If you are going to answer me something like "Because bothe belongs to different objects" then my ques will be like "if child class does not have its own copy and variable i is only present in the child class (but not have any copy) then all object of child class should have same values as it has into the parent class." i mean if i change the value into the parent class then this should also change into the all objects of the child class.


When I say that "a child class doesn't have a copy", I mean that "the child class doesn't have a copy of it in its definition" -- instances of the child class is using the instance variable defined in the super class. I am not saying that child class instances doesn't have an "i" variable.

I agree that this is a very subtle distinction, but you need to understand it -- or your other option is to just memorize the behavior by rote.

Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:If you are going to answer me something like "Because bothe belongs to different objects" then my ques will be like "if child class does not have its own copy and variable i is only present in the child class (but not have any copy) then all object of child class should have same values as it has into the parent class."


That's faulty logic.

"There are not separate copies for parent and child class"
is completely different from
"There are not separate copies for each instance".

The first is true. The second is false.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it now Thank you For Explaining Everything in Detail, Sir. This Was My First topic Here And also of my LIFE On internet. Experience is wonderful.
Once again Thank You Very Much ,Sir.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad you got it. To test your new found understanding, you can see if you can correctly predict the output of this program, and then run it to see if you were correct.


 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:I'm glad you got it. To test your new found understanding, you can see if you can correctly predict the output of this program, and then run it to see if you were correct.



Well thanks for this Wonderful Example.....
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Sagar wrote:
Jeff Verdegan wrote:I'm glad you got it. To test your new found understanding, you can see if you can correctly predict the output of this program, and then run it to see if you were correct.



Well thanks for this Wonderful Example.....


You're quite welcome! I'm glad you found it useful.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The full explanation is in the Java™ Language Specification; try starting here. It is not easy to read, I am afraid.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!