Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static variables/methods

 
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
Saloon Keeper
Posts: 15359
39
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
Saloon Keeper
Posts: 15359
39
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic