| Author |
static variables/methods
|
Isuru Buddhika
Greenhorn
Joined: Nov 04, 2012
Posts: 11
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12953
|
|
Welcome to the Ranch.
Have a look at Understanding Instance and Class Members in Oracle's Java Tutorials.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Isuru Buddhika
Greenhorn
Joined: Nov 04, 2012
Posts: 11
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12953
|
|
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).
|
 |
 |
|
|
subject: static variables/methods
|
|
|