10

Can someone give an easily comprehensible definition of a static variable and a static method?

How do these compare to non-static variables and methods?

0

5 Answers 5

15

In Java, static denotes class methods and class variables (as opposed to instance methods and instance variables). These methods and variables can be accessed without an instance present.

Contrast this to instance methods and instance variables: they must be accessed through an object. For example, length() operates on an object:

String a = "hello";
int len = a.length();

In contrast, valueOf cannot operate on an object; moreover, it creates a new object when called:

String x = String.valueOf(123.45);

Note how instance methods are called using <objectName> followed by a dot ., while static methods are accessed using <className> followed by a dot ..

7

I don't think this is an easy question to answer because it means something slightly different based on the language. If I were to put it in the most general terms which will probably wildly vary from person to person:

A static variable is a variable that is shared across all instances of a class.

A static method is a method that can be called on a class and usually does not require the class to be instantiated.

Again if I were to pick three different languages I would give three different answers.

Wikipedia might also help a bit to define these things.

http://en.wikipedia.org/wiki/Method_(computer_programming) http://en.wikipedia.org/wiki/Static_variable

1
  • I'm talking about Java in particular.
    – WAMoz56
    May 4, 2012 at 3:42
6

The 'static' keyword can be confusing because in C, where it originated, it has multiple meanings. When used to declare a variable in a function, it means that the variable has a lifetime outside of the function. It's essentially a global that is private to the function. If a global variable is static, it is essentially private to that source file. In both cases, the variable has a single memory location just like a global. It's just that the compiler prevents you from accessing it either outside the function or the compilation unit.

I presume that the word 'static' was used as the location of a static variable never changes, as opposed to a normal local variable, which will have a memory location somewhere on the stack depending on what the stack looked like when the function was called.

When C++ was created, this keyword got repurposed to refer to class-level properties and methods. I presume the thinking was that a static method or property was a sort of global that was private to the class. If you think about how these are laid out in memory, it makes a certain sort of sense as a static property is going to have a single address just like a global variable. The only difference is that the compiler doesn't allow you to use it outside the class.

Since Java (and other languages) had syntax inspired by C++, using 'static' to refer to class methods and properties is used there as well. It's unfortunate, because the use of this keyword has little relation to the English meaning.

But in general, that's the way to look at it. In most languages, if it is 'static', there's only one of them in the entire program. Think of it as something with a single, fixed memory address.

1

static variable is variable used by all the instance of a class where as in normal variables the variable is initialised again. Static methods can a significance of calling them without even creating a object.

0
1

I will show examples to understand static methods clearly.

When entering data from keyboard java uses Scanner class

ex; Scanner scn = new Scanner(System.in); int a = scn.nextInt();

So it means that nextInt() method is not static.

Static method is the method that it can be used without instantiating a class.

Look at the following example:

public class Calculate {

    static void calculatePower(int num, int pow) {
        System.out.println(Math.pow(num, pow));
    }


}

Usage of static method.

public static void main(String[] args) {
    Calculate.calculatePower(2,8);
}

So we did not instantiated the Calculate class and used calculatePower method which is static

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.