Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are static method and variables?

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?

like image 936
WAMoz56 Avatar asked May 04 '12 03:05

WAMoz56


1 Answers

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 ..

like image 159
Sergey Kalinichenko Avatar answered Oct 24 '22 03:10

Sergey Kalinichenko