aspose file tools*
The moose likes Java in General and the fly likes static variables/methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "static variables/methods" Watch "static variables/methods" New topic
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

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

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

Joined: Aug 16, 2005
Posts: 14144
    
  18

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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static variables/methods