Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Carey Brown
  • Frits Walraven

static fields, methods - overwriting?

Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that once a field was declared static, then there was only one copy of that field.  I thought the same went for methods.  I wanted to prove that to myself, and the results weren't what I expected:


I had thought that field 'a' would be re-set to 2 in class TrigClass, and that it would be 2, even when called from within class Class, if the call came after it was re-set to 2.  Also, I thought method swing() in class Class would be overwritten by the swing() in TrigClass, and there would be only one method (which would print "swing in TrigClass").  In main(), apparently, both fields (a in TrigClass and a in Class) and both methods (swing() in each class) exist independently.  If that's the case, what's the point of static?  What is unique about a field/method when they're declared static?
Ranch Hand
Posts: 139
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static simply means belonging to a class, rather than to an object. So, static method in class A, belongs to class A. And you can call it, without needing an instance of class A. So, that's the point of having static members.
inheritance is a relationship between classes, where only non-static methods/fields participate; static methods/fields have no bearing on that.
Posts: 15519
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static fields and methods get resolved to the declared type, not the actual runtime type because they dont participate in polymorphism. Read about static/early binding vs dynamic/late binding.
I will open the floodgates of his own worst nightmare! All in a tiny ad:
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic