static keyword in Java

By: Abinaya Viewed: 153546 times  Printer Friendly Format    


There will be times when you will want to define a class member that will be used independently of any object of that class. Normally a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main( ). main( ) is declared as static
because it must be called before any objects exist.

Instance variables declared as static are, essentially, global variables. When objects of its class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.

Methods declared as static have several restrictions:

  • They can only call other static methods.
  • They must only access static data.
  • They cannot refer to this or super in any way. (The keyword super relates to
    inheritance.)

If you need to do computation in order to initialize your static variables, you can declare a static block which gets executed exactly once, when the class is first loaded. The following example shows a class that has a static method, some static variables, and a static initialization block:

// Demonstrate static variables, methods, and blocks.
class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}

As soon as the UseStatic class is loaded, all of the static statements are run. First, a is set to 3, then the static block executes (printing a message), and finally, b is initialized to a * 4 or 12. Then main( ) is called, which calls meth( ), passing 42 to x. The three println( ) statements refer to the two static variables a and b, as well as to the local variable x.

Note It is illegal to refer to any instance variables inside of a static method. Here is the output of the program:

Static block initialized.
x = 42
a = 3
b = 12

Outside of the class in which they are defined, static methods and variables can be used independently of any object. To do so, you need only specify the name of their class followed by the dot operator. For example, if you wish to call a static method from outside its class, you can do so using the following general form:

classname.method( )

Here, classname is the name of the class in which the static method is declared. As you can see, this format is similar to that used to call non-static methods through object reference variables. A static variable can be accessed in the same way—by use of the dot operator on the name of the class. This is how Java implements a controlled version of global functions and global variables.
Here is an example. Inside main( ), the static method callme( ) and the static variable b are accessed outside of their class.

class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println("a = " + a);
}
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}
Here is the output of this program:
a = 42
b = 99


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. site is really good.explaining the concept with ni
View Tutorial          By: reddy chakri india at 2009-03-10 00:20:00

2. very very useful..thank you
View Tutorial          By: sridaran at 2009-03-21 00:45:21

3. realy very nice artical it give good understable
View Tutorial          By: vivek at 2009-06-19 01:27:46

4. i had a problem in running a java software jdk1.5.
View Tutorial          By: tej at 2009-06-30 08:50:27

5. this site is very useful to the freshers ...

View Tutorial          By: sweth at 2009-09-02 18:53:46

6. Really helpful way of explanation..And the example
View Tutorial          By: pavan at 2010-04-17 03:10:58

7. provided examples are very useful.
View Tutorial          By: Technical Guy at 2010-04-25 08:12:06

8. providing explanation about the topics is very use
View Tutorial          By: vikram naidu at 2010-06-09 23:40:41

9. Good one.Thank you....
View Tutorial          By: prasanna at 2010-07-23 08:36:50

10. Good one.Thank you....
View Tutorial          By: prasanna at 2010-07-23 08:36:50

11. supb yar...very helpful
View Tutorial          By: narender lakshetty at 2010-07-23 23:20:00

12. this is very nice. but iam no understand
View Tutorial          By: xxx at 2010-08-31 03:51:27

13. M finding difficulty in understanding Java. Can an
View Tutorial          By: Sunil at 2010-09-28 03:54:57

14. The site is too informative. I just like their imm
View Tutorial          By: Priyadarshi Tripathi at 2010-10-26 23:44:46

15. very very nice...
thanks

View Tutorial          By: ramu at 2010-11-23 23:38:18

16. nice
View Tutorial          By: ravi at 2010-11-26 02:46:56

17. very good example and explanation. This kind of p
View Tutorial          By: jagdish at 2011-07-03 15:15:02

18. thanks ........... its so easy and interesting to
View Tutorial          By: sammy at 2011-07-25 00:47:44

19. I am little but confusing after reading i got ever
View Tutorial          By: Shankar at 2011-08-09 10:01:03

20. I understood static keyword and thanks to the site
View Tutorial          By: palani at 2011-08-12 07:03:35

21. Very nice superb...!! good work ... thanks for adm
View Tutorial          By: santhosh at 2011-09-18 17:19:21

22. Erakyroda
View Tutorial          By: Erakyroda at 2011-09-30 09:22:30

23. Good tutorials for Beginner. Can go depth in all t
View Tutorial          By: Karthikeyan Palaniswamy at 2011-10-19 01:14:03

24. MichaelXG
View Tutorial          By: MichaelXG at 2011-10-31 06:12:20

25. im biginner of java programming, please give me in
View Tutorial          By: siva at 2011-11-04 06:40:45

26. very good explanation,very good examples..
View Tutorial          By: sra1 at 2011-11-14 08:30:47

27. excellent explanation ........ thanks a lot :)
View Tutorial          By: nitish mishra at 2011-12-05 04:29:09

28. the ans is good.now i cleared my dout.and the exam
View Tutorial          By: siva at 2011-12-07 14:00:13

29. a pretty good site for learners
View Tutorial          By: chaitra at 2011-12-15 10:09:14

30. nice tutorial with discussion
View Tutorial          By: Md Firoz at 2012-01-07 11:00:29

31. very useful...thank you....
View Tutorial          By: jiby at 2012-01-19 06:40:42

32. Thanks...it was very helpful :)
View Tutorial          By: Swathi at 2012-01-25 06:49:46

33. drearmaCymn
View Tutorial          By: drearmaCymn at 2012-02-02 11:55:26

34. really very-2 good site....

thanks

View Tutorial          By: Priti at 2012-02-06 03:00:13

35. Thnx.. it was clearly explained... tomorrow i have
View Tutorial          By: Susmitha Sen at 2012-03-25 09:34:29

36. static concept was clearly explained with some exa
View Tutorial          By: Rajkumar s ,m.p.patti,rasipuram. at 2012-03-28 16:17:31

37. now i get exactly meaning of static.really very ni
View Tutorial          By: Soni Sharma at 2012-04-12 06:24:12

38. concept of static keyword is beautifully explained
View Tutorial          By: jaideep aswal at 2012-04-14 07:21:56

39. its easily understand everyone... nice boossssss..
View Tutorial          By: Sasikumar Madurai at 2012-04-19 09:36:58

40. this is really a simplified answer that even a new
View Tutorial          By: Kannan at 2012-05-31 05:59:09

41. i can't under stand static keyword please help me.
View Tutorial          By: varinder singh at 2012-06-13 19:06:38

42. it was so nice .................i can easily solve
View Tutorial          By: pravin at 2012-07-14 06:15:33

43. Bhai,kuch innovative karo....tumne toh puri comple
View Tutorial          By: shz.zhcetian at 2012-07-29 05:08:21

44. copied from complete reference.
View Tutorial          By: ankit srivastava at 2012-08-22 21:13:14

45. write another program for using both static variab
View Tutorial          By: siva at 2012-09-11 08:58:54

46. Thanks for make me understand about the keyword St
View Tutorial          By: Soumya at 2012-09-18 03:30:01

47. thanks to this site which get me understand about
View Tutorial          By: Sayak at 2012-10-08 15:57:25

48. Nice info. I must say about this post.
than

View Tutorial          By: Ganesh Prasad at 2012-10-30 05:51:49

49. site is good.explaining the concept with nice pro
View Tutorial          By: AJOY KUMAR MANDAL at 2012-11-22 01:57:26

50. excellent explanation...............Thanks for mak
View Tutorial          By: Kiran Leo at 2012-12-08 06:41:26

51. Thank you , concept of static is clearly explained
View Tutorial          By: Jagan at 2012-12-10 10:42:42

52. Thanks ...it is really nice .superb site to learn
View Tutorial          By: hari at 2012-12-12 07:41:16

53. very nice explanation of STATIC....HAY man , you j
View Tutorial          By: harkeet at 2012-12-18 17:48:43

54. thank you very much....
View Tutorial          By: divya at 2013-01-06 15:50:43

55. Nicely explained concept. Thankx.
View Tutorial          By: Raghvendra at 2013-01-17 07:48:18

56. thank you very much...
Explained in a simpl

View Tutorial          By: vineela at 2013-02-18 10:29:08

57. site is very good explanation is very clear throug
View Tutorial          By: sridevi at 2013-03-20 15:26:29

58. This code is completely copied from Java Complete
View Tutorial          By: Rahul at 2013-04-09 08:24:30

59. good resource
View Tutorial          By: AJITH C K at 2013-04-10 17:45:51

60. Nice Tips thank you
View Tutorial          By: Giritharan at 2013-04-11 09:24:37

61. good article that help us
View Tutorial          By: D.md.Rafi at 2013-04-28 02:28:58

62. good article that help us
View Tutorial          By: D.md.Rafi at 2013-04-28 02:30:18

63. nice explanation,i am beginner of java.i learnt ma
View Tutorial          By: kamal at 2013-04-29 18:08:40

64. the best site i have found so far for clearing my
View Tutorial          By: darkknight at 2013-06-05 05:54:10

65. its very helpful..thank you..
View Tutorial          By: roja at 2013-06-16 12:37:32

66. thank's a lot for giving me " static "
View Tutorial          By: anwar kotawdekar at 2013-08-14 06:43:34

67. very use full.....
View Tutorial          By: janarthanam at 2015-08-29 06:53:03

68. Check this one to know more about...static keyword
View Tutorial          By: balmer at 2017-03-13 06:21:52

69. inetryconydot
View Tutorial          By: inetryconydot at 2017-04-16 00:22:58

70. Brettkip
View Tutorial          By: Brettkip at 2017-06-05 00:44:19