• 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

instance varibles and static methods????

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

plz refer this code:


Ques. Why does call to instance vaiable i on object ref of super class, but instance of sub class, give the o/p 0??? how this happens?? is this decided at compile time??? I know variables are not called polymorphically, but why this is so???
At the same time call to the static method is also not polymorphic but that is understood.

thanx
amit
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The instance variables and static members are decided at compile time , only instance methods are decided at runtime , that is why the output is so .
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Rohan,

ur answer is just a reinvention of the wheel...........
i intended on asking why its so(why such behavoir has been made to exhibit)??

thanx
amit
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS

15.28 Constant Expression
ConstantExpression:
Expression
A compile-time constant expression is an expression denoting a value of
primitive type or a String that is composed using only the following:

� Literals of primitive type and literals of type String


Since the member variable i in class Amit is a compile time constant it is resolved at compile time.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the static method is not actually overridden, as you expect it to be.remove the static modifier and you'll see the result after overridding takes place.
 
Pooja Shankar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the static method is not actually overridden, as you expect it to be.remove the static modifier and you'll see the result after overridding takes place.
 
It runs on an internal combustion engine. This ad does not:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic