simple static concept in java


SUBMITTED BY: coolvi4u

DATE: Nov. 27, 2015, 7:02 a.m.

FORMAT: Java

SIZE: 6.5 kB

HITS: 10178

  1. simple static concept in java :(
  2. Java static keyword
  3. Static variable
  4. Program of counter without static variable
  5. Program of counter with static variable
  6. Static method
  7. Restrictions for static method
  8. Why main method is static ?
  9. Static block
  10. Can we execute a program without main method ?
  11. The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.
  12. The static can be:
  13. variable (also known as class variable)
  14. method (also known as class method)
  15. block
  16. nested class
  17. 1) Java static variable
  18. If you declare any variable as static, it is known static variable.
  19. The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
  20. The static variable gets memory only once in class area at the time of class loading.
  21. Advantage of static variable
  22. It makes your program memory efficient (i.e it saves memory).
  23. Understanding problem without static variable
  24. class Student{
  25. int rollno;
  26. String name;
  27. String college="ITS";
  28. }
  29. Suppose there are 500 students in my college, now all instance data members will get memory each time when object is created.All student have its unique rollno and name so instance data member is good.Here, college refers to the common property of all objects.If we make it static,this field will get memory only once.
  30. Java static property is shared to all objects.
  31. Example of static variable
  32. //Program of static variable
  33. class Student8{
  34. int rollno;
  35. String name;
  36. static String college ="ITS";
  37. Student8(int r,String n){
  38. rollno = r;
  39. name = n;
  40. }
  41. void display (){System.out.println(rollno+" "+name+" "+college);}
  42. public static void main(String args[]){
  43. Student8 s1 = new Student8(111,"Karan");
  44. Student8 s2 = new Student8(222,"Aryan");
  45. s1.display();
  46. s2.display();
  47. }
  48. }
  49. Test it Now
  50. Output:111 Karan ITS
  51. 222 Aryan ITS
  52. Static Variable
  53. Program of counter without static variable
  54. In this example, we have created an instance variable named count which is incremented in the constructor. Since instance variable gets the memory at the time of object creation, each object will have the copy of the instance variable, if it is incremented, it won't reflect to other objects. So each objects will have the value 1 in the count variable.
  55. class Counter{
  56. int count=0;//will get memory when instance is created
  57. Counter(){
  58. count++;
  59. System.out.println(count);
  60. }
  61. public static void main(String args[]){
  62. Counter c1=new Counter();
  63. Counter c2=new Counter();
  64. Counter c3=new Counter();
  65. }
  66. }
  67. Test it Now
  68. Output:1
  69. 1
  70. 1
  71. Program of counter by static variable
  72. As we have mentioned above, static variable will get the memory only once, if any object changes the value of the static variable, it will retain its value.
  73. class Counter2{
  74. static int count=0;//will get memory only once and retain its value
  75. Counter2(){
  76. count++;
  77. System.out.println(count);
  78. }
  79. public static void main(String args[]){
  80. Counter2 c1=new Counter2();
  81. Counter2 c2=new Counter2();
  82. Counter2 c3=new Counter2();
  83. }
  84. }
  85. Test it Now
  86. Output:1
  87. 2
  88. 3
  89. 2) Java static method
  90. If you apply static keyword with any method, it is known as static method.
  91. A static method belongs to the class rather than object of a class.
  92. A static method can be invoked without the need for creating an instance of a class.
  93. static method can access static data member and can change the value of it.
  94. Example of static method
  95. //Program of changing the common property of all objects(static field).
  96. class Student9{
  97. int rollno;
  98. String name;
  99. static String college = "ITS";
  100. static void change(){
  101. college = "BBDIT";
  102. }
  103. Student9(int r, String n){
  104. rollno = r;
  105. name = n;
  106. }
  107. void display (){System.out.println(rollno+" "+name+" "+college);}
  108. public static void main(String args[]){
  109. Student9.change();
  110. Student9 s1 = new Student9 (111,"Karan");
  111. Student9 s2 = new Student9 (222,"Aryan");
  112. Student9 s3 = new Student9 (333,"Sonoo");
  113. s1.display();
  114. s2.display();
  115. s3.display();
  116. }
  117. }
  118. Test it Now
  119. Output:111 Karan BBDIT
  120. 222 Aryan BBDIT
  121. 333 Sonoo BBDIT
  122. Another example of static method that performs normal calculation
  123. //Program to get cube of a given number by static method
  124. class Calculate{
  125. static int cube(int x){
  126. return x*x*x;
  127. }
  128. public static void main(String args[]){
  129. int result=Calculate.cube(5);
  130. System.out.println(result);
  131. }
  132. }
  133. Test it Now
  134. Output:125
  135. Restrictions for static method
  136. There are two main restrictions for the static method. They are:
  137. The static method can not use non static data member or call non-static method directly.
  138. this and super cannot be used in static context.
  139. class A{
  140. int a=40;//non static
  141. public static void main(String args[]){
  142. System.out.println(a);
  143. }
  144. }
  145. Test it Now
  146. Output:Compile Time Error
  147. Q) why java main method is static?
  148. Ans) because object is not required to call static method if it were non-static method, jvm create object first then call main() method that will lead the problem of extra memory allocation.
  149. 3) Java static block
  150. Is used to initialize the static data member.
  151. It is executed before main method at the time of classloading.
  152. Example of static block
  153. class A2{
  154. static{System.out.println("static block is invoked");}
  155. public static void main(String args[]){
  156. System.out.println("Hello main");
  157. }
  158. }
  159. Test it Now
  160. Output:static block is invoked
  161. Hello main
  162. Q) Can we execute a program without main() method?
  163. Ans) Yes, one of the way is static block but in previous version of JDK not in JDK 1.7.
  164. class A3{
  165. static{
  166. System.out.println("static block is invoked");
  167. System.exit(0);
  168. }
  169. }
  170. Test it Now
  171. Output:static block is invoked (if not JDK7)
  172. In JDK7 and above, output will be:
  173. Output:Error: Main method not found in class A3, please define the main method as:
  174. public static void main(String[] args)

comments powered by Disqus