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/methods  RSS feed

 
Isuru Buddhika
Greenhorn
Posts: 11
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the different between a
1) static variable and a non static variable
2) static method and a non static method.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Have a look at Understanding Instance and Class Members in Oracle's Java Tutorials.
 
Isuru Buddhika
Greenhorn
Posts: 11
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Welcome to the Ranch.

Have a look at Understanding Instance and Class Members in Oracle's Java Tutorials.


Thanks!

On a lecture I was told that non static methods/variables exists in the object that get created in the heap while the static ones stays in the .class file that get loaded to the RAM. Is that correct?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isuru Buddhika wrote:On a lecture I was told that non static methods/variables exists in the object that get created in the heap while the static ones stays in the .class file that get loaded to the RAM. Is that correct?

No, that doesn't sound correct.

The difference is essentially this: For normal, non-static variables, each instance (object) of the class has its own copy. For static variables, there is only one variable, which is shared by all instances (objects) of the class. So, a non-static variable is an instance variable and a static variable is a class variable, as the Oracle tutorial explains.

Likewise for methods: non-static methods work on objects, static methods are for the whole class.

Don't worry about where in memory what variables are stored. That's not the important part of the concept to understand (and how it works exactly isn't even specified; it's up to the specific implementation of the JVM).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!