• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instance varibles and static methods????

 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • 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
 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • 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.
 
Pooja Shankar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic