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 ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

static vs non static

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


I was just not clear as to how does the compiler handle Static members.

As far as fields are concerned if they are declared static, they are created on the HEAP and are shared by all the class instances.

However, what i want to know is what happenes to the methods. I mean methods are just piece of code so why should a non-static method be instantiated for all the instances?

So, I presume that static members mean that they are created on the HEAP.

Am i right here?


Thanks

Praveen
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

static members relate to the class, non-static members relate to the instance.

For fields, this means that a static field is shared by all instances of the class while a non-static field is specific to an instance.

Non-static methods are called on an instance and have access to non-static fields of that instance (and, of course, static fields of the class). Static methods can be called without having an instance; static methods only have access to static fields.

Forget about the heap in this discussion ;-)

Check the Java Tutorial for details.

Cheers,
Oliver
 
What's wrong? Where are you going? Stop! Read this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic