Advertisement
fillpant

Untitled

Jan 11th, 2017
24,427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. Private:
  2. Variables, Methods:
  3. * Can only be accessed by members of the class they belong to:
  4. class TestClass{
  5. private int number = 4;
  6.  
  7. public int getNumber(){
  8. return number;
  9. }
  10.  
  11. public void addOneToNumber(){
  12. number = number + 1;
  13. }
  14. }
  15.  
  16. * Can not be accessed from other classes:
  17. class SecondTestClass{
  18.  
  19. public void tryToAccessPrivate(){
  20. TestClass test = new TestClass();
  21. test.number = 1000; //Will error.
  22. }
  23.  
  24. }
  25.  
  26. Class:
  27. * Are not stand-alone classes.
  28. * Are classes inside other classes.
  29. * Can only be accessed by the class they are in to.
  30.  
  31. Public:
  32. Variables, Methods:
  33. * Can be accessed from all other classes
  34. * Public variables are discouraged as they ruin the concept of Encapsulation
  35. * Variables are made private in classes typically, and public methods called 'getters' and 'setters' are created with the purpose of 'interfacing' the variables. This is called Encapsulation.
  36. * Methods are declared public if and only if there is a reason why they need to be accessible from other classes. Do not make all methods public.
  37.  
  38.  
  39. Comparison:
  40. Public permits access to variables, methods, and classes from other classes regardless of their package and/or other properties whereas private declares the method/variable or class only accessible from the class surrounding it and other members of this class.
  41. Use of public for variables is discouraged as it does not follow the fundamental OOP concept of encapsulation.
  42.  
  43.  
  44.  
  45. Static:
  46. Whatever is marked as 'static' belongs to the class and not to an object, and will remain the same for all objects
  47. In further detail if you have a static variable in a class, it will be the same for all objects of this class!
  48. For example:
  49. class FirstClass{
  50. public static int staticNumber = 110;
  51. public int nonStaticNumber = 550;
  52.  
  53. public static void staticMethodPrintHello(){
  54. System.out.println("Hello");
  55. }
  56.  
  57. public void nonStaticMethodPrintHey(){
  58. System.out.println("Hey");
  59. }
  60.  
  61. }
  62.  
  63. class SecondClass{
  64.  
  65. public static void main(String[] args){
  66. System.out.println(FirstClass.staticNumber); //access 'number' from FirstClass which is static
  67.  
  68. FirstClass instance = new FirstClass();
  69. System.out.println(instance.nonStaticNumber);//accessing non static variable requires an instance.
  70.  
  71. // Methods:
  72. FirstClass.staticMethodPrintHello();//using a static method requires no Object.
  73.  
  74. FirstClass instanc = new FirstClass();
  75. instanc.nonStaticMethodPrintHey(); //using a non static method requires an Object (instanc)
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement