static members based on java tutorial by oracle: svars.html

17
Static members Based on Java tutorial by Oracle: http://docs.oracle.com/javase/tutorial /java/javaOO/classvars.html And on: http://www.javatutorialhub.com/java- static-variable-methods.html

Upload: paula-newton

Post on 21-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Static members Based on Java tutorial by Oracle:  svars.html

Static members

Based on Java tutorial by Oracle: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.htmlAnd on: http://www.javatutorialhub.com/java-static-variable-methods.html

Page 2: Static members Based on Java tutorial by Oracle:  svars.html

Motivation

• The fields we’ve encountered so far are unique for every object instantiated.

• Sometimes there’s a need for a datadata or a behaviorbehavior which is shared by all instances 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

Page 3: Static members Based on Java tutorial by Oracle:  svars.html

Static fields• Fields that have the static modifier in their

declaration are called static fieldsstatic fields or class class variables.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

Page 4: Static members Based on Java tutorial by Oracle:  svars.html

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

Page 5: Static members Based on Java tutorial by Oracle:  svars.html

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;

... }

Page 6: Static members Based on Java tutorial by Oracle:  svars.html

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 numberid = ++numberOfBicycles;

}

public int getID() {return id;

}

Page 7: Static members Based on Java tutorial by Oracle:  svars.html

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

Page 8: Static members Based on Java tutorial by Oracle:  svars.html

Static methods

• The Java programming language supports static methods as well as static variables.

• mainmain 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 inClassName.methodName(args)

Introduction to Computer Science, Ben-Gurion University of the Negev 8

Page 9: Static members Based on Java tutorial by Oracle:  svars.html

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;

}

Page 10: Static members Based on Java tutorial by Oracle:  svars.html

Static methodsNot 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.•Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this this or supersuper keywords, as there is no instance to refer to.

Introduction to Computer Science, Ben-Gurion University of the Negev 10

Page 11: Static members Based on Java tutorial by Oracle:  svars.html

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

Page 12: Static members Based on Java tutorial by Oracle:  svars.html

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

Page 13: Static members Based on Java tutorial by Oracle:  svars.html

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

Page 14: Static members Based on Java tutorial by Oracle:  svars.html

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

Page 15: Static members Based on Java tutorial by Oracle:  svars.html

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;

}

...}

Page 16: Static members Based on Java tutorial by Oracle:  svars.html

Summary

static variablestatic variable•It is a variable which belongs to thebelongs to the classclass and notnot to objectobject (instance).•Static variables are initialized only onceinitialized only once , at the start of the execution. These variables will be initialized first, before the initialization of any instance variables.•A singlesingle copycopy to be shared by all instances of the class.•A static variable can be accessedaccessed directlydirectly by the classclass namename and doesn’t need any objectSyntax : <class-name>.<variable-name>

Introduction to Computer Science, Ben-Gurion University of the Negev 16

Page 17: Static members Based on Java tutorial by Oracle:  svars.html

Summary

static methodstatic method•It is a method which belongsbelongs toto the classthe class and notnot to the object object (instance).•A static method can access only static datacan access only static data. It can not access non-static data (instance variables).•A static method can call onlycan call only other static methodsstatic methods and can not call a non-static method from it.•A static method cannot refer to "thisthis" or "supersuper" keywords in anyway.•A static method can be accessed directlyaccessed directly by the class class namename and doesn’t need any objectSyntax : <class-name>.<method-name>

Introduction to Computer Science, Ben-Gurion University of the Negev 17