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., 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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(); } }
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(); } }
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(); } }
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(); } }
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."); }
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."); }
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."); }
Recommend
More recommend