Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
93 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...