Download presentation
Presentation is loading. Please wait.
Published byPaula Newton Modified over 9 years ago
1
Static members Based on Java tutorial by Oracle: http://docs.oracle.com/javase/tutorial/java/javaOO/clas svars.html http://docs.oracle.com/javase/tutorial/java/javaOO/clas svars.html And on: http://www.javatutorialhub.com/java-static-variable- methods.html
2
Motivation The fields we’ve encountered so far are unique for every object instantiated. data behaviorall instances Sometimes there’s a need for a data or a behavior which is shared by all instances of a class. For example: counting the number of instances of a certain class in the program. Introduction to Computer Science, Ben- Gurion University of the Negev 2
3
Static fields static fieldsclass variables. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class. Introduction to Computer Science, Ben- Gurion University of the Negev 3
4
Example For example, suppose you want to create a number of Bicycle objects and assign each a serial number. This ID number is unique to each object and is therefore an instance variable. At the same time, you need a field to keep track of how many Bicycle objects have been created so that you know what ID to assign to the next one. Such a field is not related to any individual object, but to the class as a whole. Introduction to Computer Science, Ben- Gurion University of the Negev 4
5
Example cont’d Introduction to Computer Science, Ben- Gurion University of the Negev 5 public class Bicycle{ private int cadence; private int gear; private int speed; // An instance variable for the object ID private int id; // A class variable for the number of Bicycle objects instantiated private static int numberOfBicycles = 0;... }
6
Example cont’d Introduction to Computer Science, Ben- Gurion University of the Negev 6 public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; // increment number of Bicycles and assign ID number id = ++numberOfBicycles; } public int getID() { return id; }
7
Example cont’d Class variables are referenced by the class name itself, as in Bicycle.numberOfBicycles This makes it clear that they are class variables. Reference through an object is possible, but is discouraged. Introduction to Computer Science, Ben- Gurion University of the Negev 7
8
Static methods The Java programming language supports static methods as well as static variables. main main method is static, since it must be accessible for an application to run, before any instantiation takes place. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in ClassName.methodName(args) Introduction to Computer Science, Ben- Gurion University of the Negev 8
9
Static methods A common use for static methods is to access static fields. For example, we could add a static method to the Bicycle class to access the static field numberOfBicycles : Introduction to Computer Science, Ben- Gurion University of the Negev 9 public static int getNumberOfBicycles() { return numberOfBicycles; }
10
Static methods Not all combinations of instance and class variables and methods are allowed: Instance methods can access instance variables and instance methods directly. Instance methods can access class variables and class methods directly. Class methods can access class variables and class methods directly. this super Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this or super keywords, as there is no instance to refer to. Introduction to Computer Science, Ben- Gurion University of the Negev 10
11
Constants Constant fields are often declared as static. For example NUM_OF_WHEELS which is a characteristic of any Bicycle, and not of a certain instance. If there’s a sense to expose a constant field to the outside world, it is common to declare the field as public, rather than through a getter. Introduction to Computer Science, Ben- Gurion University of the Negev 11
12
Singleton pattern Restricting the instantiation of a certain class to exactly one object. This is useful when exactly one object is needed to coordinate actions across the system. A singleton object is accessible globally using a static method. Introduction to Computer Science, Ben- Gurion University of the Negev 12
13
Singleton Introduction to Computer Science, Ben- Gurion University of the Negev 13 public class Controller { private static Controller instance = null; private Controller () {... } public static Controller getInstance() { if (instance == null) { instance = new Controller (); } return instance; }... } * Not thread-safe Notice the private constructor
14
A static block The static block, is a block of statement inside a Java class that will be executed when a class is first loaded in to the JVM. A static block helps to initialize the static data members, just like constructors help to initialize instance members. Introduction to Computer Science, Ben- Gurion University of the Negev 14
15
A static block Introduction to Computer Science, Ben- Gurion University of the Negev 15 public class Controller { private static Controller instance; static { instance = new Controller (); } private Controller () { } public static Controller getInstance() { return instance; }... }
16
Summary static variable belongs to theclassnot object It is a variable which belongs to the class and not to object (instance). initialized only once Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables. singlecopy A single copy to be shared by all instances of the class. accesseddirectlyclass name A static variable can be accessed directly by the class name and doesn’t need any object Syntax :. Introduction to Computer Science, Ben- Gurion University of the Negev 16
17
Summary static method belongs to the classnot object It is a method which belongs to the class and not to the object (instance). can access only static data A static method can access only static data. It can not access non-static data (instance variables). can call onlystatic methods A static method can call only other static methods and can not call a non-static method from it. thissuper A static method cannot refer to "this" or "super" keywords in anyway. accessed directlyclass name A static method can be accessed directly by the class name and doesn’t need any object Syntax :. Introduction to Computer Science, Ben- Gurion University of the Negev 17
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.