Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Created by Eng: Mohammed Alokshiya Modified by Eng: Mohammed Abdualal Lab 11 Object Oriented Programming (II) Eng. Mohammed Abdualal November 29, 2015
Static Variables and Methods The data field age in the Person class is known as an instance variable. An instance variable is tied to a specific instance of the class; it is not shared among objects of the same class. For example, suppose that you create the following objects: Person person1 = new Person("Mohammed", "Ahmed", 23, 120191379); Person person2 = new Person("Ahmed", "Khaled", 19, 120140000); The age in person1 is independent of the age in person2 and is stored in a different memory location. Changes made to person1 s age do not affect person2 s age, and vice versa. If you want all the instances of a class to share data, use static variables, also known as class variables. Static variables store values for the variables in a common memory location. Because of this common location, if one object changes the value of a static variable, all objects of the same class are affected. Java supports static methods as well as static variables. Static methods can be called without creating an instance of the class. Let s modify the Person class by adding a static variable counter to count the number of person objects created. When the first object of this class is created, counter is 1. When the second object is created, counter becomes 2. The constructor methods are guaranteed to be invoked once when creating a new object, so we increment the counter in the constructors. class Person { // Fields String fname; String lname; int age; int ID; static int counter = 0; class Person Static field // A Constructor public Person() { counter++; Increment the counter when creating a new object using first constructor // Another Constructor public Person(String fname, String lname, int age, int ID) { 2
this.fname = fname; this.lname = lname; this.age = age; this.id = ID; counter++; Now the counter field will be incremented when you create any object of class Person. To print the value of counter, get it with the class Name: System.out.println(Person.count); Increment the counter when creating a new object using second constructor You can also define static methods. Static methods can be invoked with class name, without creating objects. An instance method can invoke an instance or static method and access an instance or static data field. A static method can invoke a static method and access a static data field. However, a static method cannot invoke an instance method or access an instance data field, since static methods and static data fields do not belong to a particular object. The relationship between static and instance members is summarized in the following diagram: public static int getcounter() { return counter; Static Method 3
Visibility Modifiers The visibility (access) modifiers in java specifies accessibility (scope) of a data member, method, constructor or class. There are 4 types of java access modifiers: private default protected public Private access modifier: The private access modifier is accessible only within class. In the following example, we have created two classes A and Simple. A class contains private data member and private method. We are accessing these private members from outside the class, so there is compilation error. class A { Simple.java private int data = 40; private void msg() { System.out.println("Hello java"); public class Simple { public static void main(string args[]) { A obj = new A(); System.out.println(obj.data); // Compilation Error obj.msg(); // Compilation Error Default access modifier: If you do not use any modifier, it is treated as default by default. The default modifier is accessible only within package. In the following example, we have created two packages pack and mypack. We are accessing the msg() method in obj object, which is an instance of A class from outside its 4
package, since msg() method is not public, so it cannot be accessed from outside the package. A.java package pack; public class A { void msg() { System.out.println("Hello"); B.java package mypack; import pack.a; public class B { public static void main(string args[]) { A obj = new A(); obj.msg();// Compilation Error In the above example, the scope of method msg() is default so it cannot be accessed from outside the package. Protected access modifier: Protected access modifier will be explained later, when you start working with inheritance and super classes. Public access modifier: The public access modifier is accessible everywhere. It has the widest scope among all other modifiers. Example: A.java package pack; public class A { public void msg() { System.out.println("Hello"); B.java package mypack; import pack.a; public class B { public static void main(string args[]) { A obj = new A(); obj.msg(); 5
Hello Output Summary: Visibility Modifiers Summary Modifier Class Package Subclass World + + + + public y y y y + + + + protected y y y n + + + + no modifier y y n n + + + + private y n n n y: accessible n: not accessible Subclass and protected modifier will be explained in details in Java II. 6
Encapsulation The whole idea behind encapsulation is to hide the implementation details from users. If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (fields) of other class. However if we setup public getter and setter methods to update (for e.g. void setage(int age)) and read (for e.g. int getage()) the private data fields then the outside class can access those private data fields via public methods. This way data can only be accessed by public methods thus making the private fields and their implementation hidden for outside classes. That s why encapsulation is known as data hiding. Lets see an example to understand this concept better. class Person { Person.java // Fields private String fname; private String lname; private int age; private int ID; private static int counter = 0; // Only Getter Method for static field counter // to guarantee that there is no way to change its value // except creating an instance public static int getcounter() { return counter; // A Constructor public Person() { counter++; // Another Constructor public Person(String fname, String lname, int age, int ID) { this.fname = fname; this.lname = lname; this.age = age; this.id = ID; counter++; // Getters and Setters Methods public String getfname() { return fname; 7
public void setfname(string fname) { this.fname = fname; public String getlname() { return lname; public void setlname(string lname) { this.lname = lname; public int getage() { return age; public void setage(int age) { this.age = age; public int getid() { return ID; public void setid(int ID) { this.id = ID; 8
Netbeans Tips & Tricks Code Generation Right click on Edit pane and select Insert Code, or simply press (ALT + INSERT) Then choose what you want to generate. For example, generate Setters and Getters for the class fields: 9
A new window will appear to let you choose the fields that you want to generate setters and getters for them. Moreover, you can use the same window to auto-encapsulate the fields: After you click generate, a new methods will be generated in the class: 10
You can also use Code Generator to generate other methods, like a new constructor(s): 11
Result: 12