static keyword in Java

By: Abinaya  

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



Archived Comments

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

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

3. Check this one to know more about...static keyword

file:///D:/rajesh/WORKSHOP/intoWeb

View Tutorial          By: balmer at 2017-03-13 06:21:52

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

5. thank's a lot for giving me " static " meaning
i understand clearly and concise

View Tutorial          By: anwar kotawdekar at 2013-08-14 06:43:34

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

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

8. nice explanation,i am beginner of java.i learnt many things clearly from this site and www.java2all.
View Tutorial          By: kamal at 2013-04-29 18:08:40

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

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

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

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

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

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

15. thank you very much...
Explained in a simple manner with example...

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

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

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

18. very nice explanation of STATIC....HAY man , you just cleared my doubts..thnks...
View Tutorial          By: harkeet at 2012-12-18 17:48:43

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

20. Thank you , concept of static is clearly explained in the above examples. Keep doing the good work.
View Tutorial          By: Jagan at 2012-12-10 10:42:42

21. excellent explanation...............Thanks for making me understand about the keyword Static
View Tutorial          By: Kiran Leo at 2012-12-08 06:41:26

22. site is good.explaining the concept with nice programs is the one i impressed with this site.

View Tutorial          By: AJOY KUMAR MANDAL at 2012-11-22 01:57:26

23. Nice info. I must say about this post.
thank you for sharing with us.

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

24. thanks to this site which get me understand about the staitc in java and why we use static also.....
View Tutorial          By: Sayak at 2012-10-08 15:57:25

25. Thanks for make me understand about the keyword Static. I have a doubt "what will be the resul
View Tutorial          By: Soumya at 2012-09-18 03:30:01

26. write another program for using both static variable and instance variable
View Tutorial          By: siva at 2012-09-11 08:58:54

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

28. Bhai,kuch innovative karo....tumne toh puri complete reference in java copy paste kardi
View Tutorial          By: shz.zhcetian at 2012-07-29 05:08:21

29. it was so nice .................i can easily solved my doubt's......thank you.......................
View Tutorial          By: pravin at 2012-07-14 06:15:33

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

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

32. its easily understand everyone... nice boossssss.... by dopri thala lawrence
View Tutorial          By: Sasikumar Madurai at 2012-04-19 09:36:58

33. concept of static keyword is beautifully explained..
thank you very much sir...

View Tutorial          By: jaideep aswal at 2012-04-14 07:21:56

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

35. static concept was clearly explained with some examples above.it is very helpful for my interviews.
View Tutorial          By: Rajkumar s ,m.p.patti,rasipuram. at 2012-03-28 16:17:31

36. Thnx.. it was clearly explained... tomorrow i have my computer board exam.. its very helpful..
View Tutorial          By: Susmitha Sen at 2012-03-25 09:34:29

37. really very-2 good site....

thanks a lot..

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

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

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

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

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

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

43. the ans is good.now i cleared my dout.and the exampls are very simple and understandable.and it look
View Tutorial          By: siva at 2011-12-07 14:00:13

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

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

46. im biginner of java programming, please give me information about static(satic blocks,members,classe
View Tutorial          By: siva at 2011-11-04 06:40:45

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

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

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

50. Very nice superb...!! good work ... thanks for admin ot this website and thanks for author of the c
View Tutorial          By: santhosh at 2011-09-18 17:19:21

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

52. I am little but confusing after reading i got everything about static
View Tutorial          By: Shankar at 2011-08-09 10:01:03

53. thanks ........... its so easy and interesting to learn java through this site.
View Tutorial          By: sammy at 2011-07-25 00:47:44

54. very good example and explanation. This kind of pretty examples needed for the beginner to understa
View Tutorial          By: jagdish at 2011-07-03 15:15:02

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

56. very very nice...
thanks

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

57. The site is too informative. I just like their immaculate way of explaining the topics with proper u
View Tutorial          By: Priyadarshi Tripathi at 2010-10-26 23:44:46

58. M finding difficulty in understanding Java. Can any1 teach me. Please i am a B.A. student. I no how
View Tutorial          By: Sunil at 2010-09-28 03:54:57

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

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

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

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

63. providing explanation about the topics is very useful and example are more helpful to understand the
View Tutorial          By: vikram naidu at 2010-06-09 23:40:41

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

65. Really helpful way of explanation..And the examples also giving much information...Thanx...
View Tutorial          By: pavan at 2010-04-17 03:10:58

66. this site is very useful to the freshers ...
with these examples we can easily understand wh

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

67. i had a problem in running a java software jdk1.5.0 which has already been installed can someone hel
View Tutorial          By: tej at 2009-06-30 08:50:27

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

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

70. site is really good.explaining the concept with nice programs is the one i impressed with this site.
View Tutorial          By: reddy chakri india at 2009-03-10 00:20:00


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial