Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Access Specifiers Public Private Protected and Static ,Non static member function and member variable in Java

  1. import java.io.*;
  2.  
  3. class student {
  4.  
  5. static int count;//Does not Require initialization.
  6. int count1 = 0;//Non Static Member Variable must to be initialized
  7.  
  8. student() {
  9. acc_details();//Accessed only within the class
  10. }
  11.  
  12. public static void stu_details()//static method can be called without the object
  13. {
  14. System.out.println("Name:XXXX");
  15. System.out.println("Roll No:123456");
  16. count++;
  17. //count1++;here it cannot be used because non static member could not be used in the static function
  18. }
  19.  
  20. private void acc_details()//non static method can be called only with the object
  21. {
  22. System.out.println("Entered into Private Access Specifiers");
  23. System.out.println("Password:*******");
  24. count++;
  25. count1++;
  26. }
  27.  
  28. protected void att_details()//non static method can be called only with the object
  29. {
  30. System.out.println("Attendance Details");
  31. count++;
  32. count1++;
  33.  
  34. }
  35. }
  36.  
  37. class staff extends student {
  38.  
  39. protected static void sta_details()//static method can be called without the object
  40. {
  41. count++;
  42. //count1++;here it cannot be used because non static member could not be used in the static function
  43. System.out.println("Name:YYYY");
  44.  
  45. }
  46. }
  47.  
  48. class hod extends staff {
  49.  
  50. protected static void hod_details()//static method can be called without the object
  51. {
  52. count++;
  53.  
  54. //count1++;here it cannot be used because non static member could not be used in the static function
  55.  
  56. System.out.println("Name:ZZZ");
  57.  
  58. }
  59. }
  60.  
  61. class college extends hod {
  62.  
  63. public static void main(String a[]) {
  64.  
  65.  
  66. stu_details();//College can view the student details because it is in public mode
  67. /*
  68. * static method can be called without the object .If you didnt specify
  69. * as static during the declaration of this function you will get an
  70. * error message during the calling of this function
  71. */
  72.  
  73. sta_details();//College can view the staff details because it is in public mode
  74. /*
  75. * static method can be called without the object .If you didnt specify
  76. * as static during the declaration of this function you will get an
  77. * error message during the calling of this function
  78. */
  79.  
  80. hod_details();//College can view the hod details because it is in public mode
  81. /*
  82. * static method can be called without the object .If you didnt specify
  83. * as static during the declaration of this function you will get an
  84. * error message during the calling of this function
  85. */
  86.  
  87.  
  88. staff s1 = new staff();
  89. s1.stu_details();//staff can also view the student details because it is in public mode
  90.  
  91.  
  92. hod s2 = new hod();
  93. s2.stu_details();//hod can also view the student details because it is in public mode
  94.  
  95. s1.att_details();//staff can also view the student attendance details because it is inherited so it has an access over protected details.
  96.  
  97.  
  98. s2.att_details();//staff can also view the student attendance details because it is inherited so it has an access over protected details.
  99.  
  100.  
  101. //acc_details() cannot not be viewed by any of the classes like staff,hod and college becuase it is in private mode.
  102. student s = new student();
  103. //s.acc_details(); it cannot be called because private mode function only accessed within the function.
  104. s.stu_details();
  105. s.att_details();
  106.  
  107. System.out.println("Count value without object:" + count);//count variable can be called without an object
  108. //System.out.println("Count1 Value:" + count1); count1 variable cannot be called without an object because it is non-static
  109. System.out.println("Count value with object of class student:" + s.count);
  110. System.out.println("Count value with object of class staff:" + s1.count);
  111. System.out.println("Count value with object of class hod:" + s2.count);
  112. System.out.println("Count1 value with object of class student:" + s.count1);
  113. System.out.println("Count1 value with object of class staff:" + s1.count1);
  114. System.out.println("Count1 value with object of class hod:" + s2.count1);
  115. }
  116. }