Islamic University of Gaza Faculty of Engineering Computer...
Transcript of Islamic University of Gaza Faculty of Engineering Computer...
Islamic University of Gaza
Faculty of Engineering
Computer Engineering Department
Computer Programming Lab (ECOM 2114)
Lab 12
Object Oriented
Programming (II)
Eng. Mohammed Alokshiya
December 23, 2014
2
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 class Person {
// Fields
String fName;
String lName;
int age;
int ID;
static int counter = 0;
// A Constructor
public Person() {
counter++;
}
// Another Constructor
public Person(String fName, String lName, int age, int ID) {
Static field
Increment the counter when creating
a new object using first constructor
()
3
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);
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:
Static Method public static int getCounter() {
return counter;
}
Increment the counter when creating
a new object using second constructor
()
4
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.
Simple.java class A {
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 package, since
5
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();
}
}
6
Output Hello
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.
7
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.
Person.java class Person {
// 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;
}
8
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;
}
}
9
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:
10
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: