This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

how does system chooses the instance/static variable's class at runtime?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the below example, when i print class of "this", it gives me SubClass (SCJPExam) name. But variable values are printed from Exam class. How does the system chooses the Exam class for getting the variable values?.

1) I understand that instance/static variables are not overridden and simply hidden in subclass.
2) If i override the method in SubClass, am able to print the values from SubClass.



Output:
level is ---->easy2
level is ---->easy2
class is ---->class SCJPExam



Source: i copied this code form some site and forgot it now.
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method invocation is decided at runtime, based on the type of object being referenced,
whereas, since instance variables are not overrided through polymorphism, hence appropriate variable is chosen based on the type of reference variable.
 
Karthikeyan Kandasamy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Is it okay to assume that "variable values are sourced from class in which the method is defined"?.

In the below example, variable "level" is declared in each class/interface.

In main function, am making the function call "printLevel" with Interface's reference but variable "level"'s value is sourced from a subclass (SCJPExam).


Testing (interface) ---> Exam (Class) -----> SCJPExam ------> SCJPExam_851




Output:
level is ---->tough
level is ---->tough
class is ---->class SCJPExam_851
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthikeyan Kandasamy wrote:Thanks. Is it okay to assume that "variable values are sourced from class in which the method is defined"?.


Always.. (If you are using this reference to access that variable)

Karthikeyan Kandasamy wrote:
In the below example, variable "level" is declared in each class/interface.
In main function, am making the function call "printLevel" with Interface's reference but variable "level"'s value is sourced from a subclass (SCJPExam).

Testing (interface) ---> Exam (Class) -----> SCJPExam ------> SCJPExam_851



Output:
level is ---->tough
level is ---->tough
class is ---->class SCJPExam_851



Try printing the variable level with the reference "t" in main..
Similarly you can create reference of class Exam and assign it to object of type SCJPExam_851
and print the variable level with that reference.. and See the output in both cases..

Instance variable never follow the rule of Overriding, but as you enter the method, you will have access to the variable in that class only through "this" reference..
Because compiler will see the smallest possible scope..
Why would you want to access variable of another class from the method defined in a different class, that too using a this reference??
 
The City calls upon her steadfast protectors. Now for a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic