Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

static vs non static  RSS feed

 
rashmi anthony
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
 
Oliver Kamps
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!