What is a static method and variables?

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

How do they compare with non-static variables and methods?

+8
java definition
source share
5 answers

In Java, static refers to class methods and class variables (as opposed to instance methods and instance variables). Access to these methods and variables is possible without the presence of an instance.

Compare this to instance methods and instance variables: they must be accessed through the object. For example, length() works with an object:

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

In contrast, valueOf cannot work with an object; in addition, it creates a new object when called:

 String x = String.valueOf(123.45); 

Notice how the instance methods are called using the <objectName> , followed by a period . , and to access static methods, use <className> , and then period . .

+15
source share

I don't think this is a simple question to answer, because it means something a little different from the language. If I put it in the most general terms that are likely to differ from person to person:

A static variable is a variable that is used for all instances of the class.

A static method is a method that can be called in a class and usually does not require an instance of the class.

Again, if I chose three different languages, I would give three different answers.

Wikipedia can also help a little to identify these things.

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

+7
source share

The keyword 'static' can be confusing because in C, where it originated, it has several meanings. When used to declare a variable in a function, it means that the variable has a lifetime outside the function. It is essentially global, which is private to the function. If the global variable is static, it is essentially private to this source file. In both cases, the variable has one memory location, as global. It is just that the compiler does not allow you to access it either outside the function or compilation.

I guess the word β€œstatic” was used because the location of the static variable never changes, unlike a regular local variable that will take place in memory somewhere on the stack depending on how the stack looked when the function was called.

When C ++ was created, this keyword was redistributed to refer to class properties and methods. I suppose the thought was that the static method or property was kind of global, which was closed to the class. If you think about how they are laid out in memory, this makes some sense, since a static property will have one address, like a global variable. The only difference is that the compiler does not allow you to use it outside the class.

Since Java (and other languages) had syntax inspired by C ++, it also uses "static" to denote class methods and properties. This is sad because the use of this keyword has little to do with the English meaning.

But overall, this is a way to look at it. In most languages, if it is "static", in one program there is only one of them. Think of it as something with one fixed memory address.

+6
source share

a static variable is a variable used by the entire instance of the class, where, as in regular variables, the variable is initialized again. Static methods can have the value of invoking them without even creating an object.

+1
source share

I will show examples for understanding static methods.

When entering data from the keyboard, java uses the Scanner class

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

So this means that the nextInt () method is not static.

A static method is a method that can be used without instantiating a class.

Take a look at the following example:

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

Using the static method.

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

Thus, we did not create an instance of the Calculate class and did not use the calculatePower method, which is static

+1
source share

All Articles