comp 213
play

COMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: - PowerPoint PPT Presentation

COMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static. Access & non-access modifiers Access modifiers help you set the level of access you want for your classes, your variables and your methods, e.g.,


  1. COMP 213 Advanced Object-oriented Programming Lecture 5 Modifiers: final and static.

  2. Access & non-access modifiers • Access modifiers help you set the level of access you want for your classes, your variables and your methods, e.g., private or public . • Java also provides a number of non-access modifiers to achieve many other functionalities: • The static modifier for creating class methods and variables. • The final modifier for finalizing the implementations of classes, methods, and variables. • The abstract modifier for creating abstract classes and methods. • The synchronized and volatile modifiers, which are used for threads.

  3. Access & non-access modifiers • Access modifiers help you set the level of access you want for your classes, your variables and your methods, e.g., private or public . • Java also provides a number of non-access modifiers to achieve many other functionalities: • The static modifier for creating class methods and variables. • The final modifier for finalizing the implementations of classes, methods, and variables. • The abstract modifier for creating abstract classes and methods. • The synchronized and volatile modifiers, which are used for threads.

  4. Access & non-access modifiers • Access modifiers help you set the level of access you want for your classes, your variables and your methods, e.g., private or public . • Java also provides a number of non-access modifiers to achieve many other functionalities: • The static modifier for creating class methods and variables. • The final modifier for finalizing the implementations of classes, methods, and variables. • The abstract modifier for creating abstract classes and methods. • The synchronized and volatile modifiers, which are used for threads.

  5. Access & non-access modifiers • Access modifiers help you set the level of access you want for your classes, your variables and your methods, e.g., private or public . • Java also provides a number of non-access modifiers to achieve many other functionalities: • The static modifier for creating class methods and variables. • The final modifier for finalizing the implementations of classes, methods, and variables. • The abstract modifier for creating abstract classes and methods. • The synchronized and volatile modifiers, which are used for threads.

  6. Access & non-access modifiers • Access modifiers help you set the level of access you want for your classes, your variables and your methods, e.g., private or public . • Java also provides a number of non-access modifiers to achieve many other functionalities: • The static modifier for creating class methods and variables. • The final modifier for finalizing the implementations of classes, methods, and variables. • The abstract modifier for creating abstract classes and methods. • The synchronized and volatile modifiers, which are used for threads.

  7. Access & non-access modifiers • Access modifiers help you set the level of access you want for your classes, your variables and your methods, e.g., private or public . • Java also provides a number of non-access modifiers to achieve many other functionalities: • The static modifier for creating class methods and variables. • The final modifier for finalizing the implementations of classes, methods, and variables. • The abstract modifier for creating abstract classes and methods. • The synchronized and volatile modifiers, which are used for threads.

  8. The static modifier • static (or class) Variables: • The static keyword is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists regardless of the number of instances of the class. • Local variables cannot be declared static : if a variable is declared inside a method in a class, it only has scope "within" the method, i.e., it is local.

  9. The static modifier • static (or class) Variables: • The static keyword is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists regardless of the number of instances of the class. • Local variables cannot be declared static : if a variable is declared inside a method in a class, it only has scope "within" the method, i.e., it is local.

  10. The static modifier • static Methods • The static keyword is used to create methods that will exist independently of any instances created for the class. • static methods do not use any instance variables of any object of the class they are defined in. • static methods take all the data from parameters and compute something from those parameters, with no reference to variables. • static variables & methods can be accessed using the class name followed by a dot and the name of the variable or method.

  11. The static modifier • static Methods • The static keyword is used to create methods that will exist independently of any instances created for the class. • static methods do not use any instance variables of any object of the class they are defined in. • static methods take all the data from parameters and compute something from those parameters, with no reference to variables. • static variables & methods can be accessed using the class name followed by a dot and the name of the variable or method.

  12. The static modifier • static Methods • The static keyword is used to create methods that will exist independently of any instances created for the class. • static methods do not use any instance variables of any object of the class they are defined in. • static methods take all the data from parameters and compute something from those parameters, with no reference to variables. • static variables & methods can be accessed using the class name followed by a dot and the name of the variable or method.

  13. The static modifier • static Methods • The static keyword is used to create methods that will exist independently of any instances created for the class. • static methods do not use any instance variables of any object of the class they are defined in. • static methods take all the data from parameters and compute something from those parameters, with no reference to variables. • static variables & methods can be accessed using the class name followed by a dot and the name of the variable or method.

  14. Example 1 public class CounterOfInstances { // Stores the total number of instances created. private static int numberOfInstances = 0; // Returns the total number of instances created. static int getCounter() { return numberOfInstances; } // Increases the total number of instances created. private static void addInstance() { numberOfInstances++; } // The default constructor CounterOfInstances() { CounterOfInstances. addInstance(); } }

  15. Example 1 public class CounterOfInstances { // Stores the total number of instances created. private static int numberOfInstances = 0; // Returns the total number of instances created. static int getCounter() { return numberOfInstances; } // Increases the total number of instances created. private static void addInstance() { numberOfInstances++; } // The default constructor CounterOfInstances() { CounterOfInstances. addInstance(); } }

  16. Example 1 public class CounterOfInstances { // Stores the total number of instances created. private static int numberOfInstances = 0; // Returns the total number of instances created. static int getCounter() { return numberOfInstances; } // Increases the total number of instances created. private static void addInstance() { numberOfInstances++; } // The default constructor CounterOfInstances() { CounterOfInstances. addInstance(); } }

  17. Example 1 public class CounterOfInstances { // Stores the total number of instances created. private static int numberOfInstances = 0; // Returns the total number of instances created. static int getCounter() { return numberOfInstances; } // Increases the total number of instances created. private static void addInstance() { numberOfInstances++; } // The default constructor CounterOfInstances() { CounterOfInstances. addInstance(); } }

  18. Example 1 public static void main(String[] args) { System. out.println("At the beginning, we have " + CounterOfInstances. getCounter() + " instances of the class."); // We create 10 new instances of the class CounterOfInstances. for (int i = 0; i < 10; i++) new CounterOfInstances(); System. out.println("We have now created " + CounterOfInstances. getCounter() + " instances of the class."); }

  19. Example 1 public static void main(String[] args) { System. out.println("At the beginning, we have " + CounterOfInstances. getCounter() + " instances of the class."); // We create 10 new instances of the class CounterOfInstances. for (int i = 0; i < 10; i++) new CounterOfInstances(); System. out.println("We have now created " + CounterOfInstances. getCounter() + " instances of the class."); }

  20. Example 1 public static void main(String[] args) { System. out.println("At the beginning, we have " + CounterOfInstances. getCounter() + " instances of the class."); // We create 10 new instances of the class CounterOfInstances. for (int i = 0; i < 10; i++) new CounterOfInstances(); System. out.println("We have now created " + CounterOfInstances. getCounter() + " instances of the class."); }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend