• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing super class instance variable

 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Added code tags - see UseCodeTags for details]

super.x should have printed 10;

and what if i mark the variable x as static.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahtab Alam wrote:super.x should have printed 10;

Why ? You set it to 12 on the previous line.
 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bit explanation will help , still didn`t get it

Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 22089
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahtab Alam wrote:A bit explanation will help , still didn`t get it

Thanks


In the previous line you set x to 12... Notice that you did *NOT* declare a local variable, nor is there an instance variable in the subclass -- so the compiler is smart enough to know that you mean to set the instance variable that was declared in the super class.

Henry
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many x variables do you think you have there?
 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was horribly wrong i thought x is instance variable in subclass since it extends .
subclass is just using the super class instance variable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic