This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Static variables and inheritance  RSS feed

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a class P and Q-extends P. P has a static string S. Both classes have a non-static method to edit S. Instances of P and Q share one static string. But, that is not the case for int. I get errors "cannot find symbol : variable <your-static-variable>" if i replace String with an int (after making the necessary changes to code).
What is happening ?

 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we're going to need to see some actual code to be much help here.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code with static int which does not compile :



The errors :


The code with static string, compiles and runs :



PS : the two thumbs up...hmm... interesting...
 
Henry Wong
author
Sheriff
Posts: 23283
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

The code with String uses inheritance. The code with int does not -- Dog goes not inherit from Animal.

Henry
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:
PS : the two thumbs up...hmm... interesting...


It means that at least two people agree with what Matthew said in the post.

Henry
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh god...sorry for that lame post...i guess that means i should be sleeping now
 
Mohamed Sanaulla
Bartender
Posts: 3179
34
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
errors like- Cannot find symblo must be easy to spot. There would be not much of logic or thinking involved in spotting them. If we try to think too much then we would get misguided and end up in a big mess.

And yeah you need to catch up with your sleep
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!