250 likes | 341 Views
Structured Programming 1401104-3 Dr. Atif Alhejali. Lecture 4 Modifiers Parameters passing. Packages. A package is a container that contains a group of classes In real world, the package is the folder that contains the classes (*.java files)
E N D
Structured Programming1401104-3Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing Structured Programming
Packages • A package is a container that contains a group of classes • In real world, the package is the folder that contains the classes (*.java files) • All classes within the same package are visible to each other • To use a class form another package it should be imported using the keyword “import”. Structured Programming
Packages • Lets assume the following example where the classes have the following hierarchy: Structured Programming
Packages • In the previous example, Class1 can be used directly while Class2 and Class3 must be imported Structured Programming
Modifiers • Modifiers are keywords used before the declaration of a variable or method to control who can see and use it • For example : • public int x; • protected double y; • Public void print(){…} Structured Programming
Modifiers • There are 4 modifiers (private, protected, public and using no modifiers) • The next table shows the visibility of each modifier. Structured Programming
Modifiers • Lets assume the following example Structured Programming
Modifiers • The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them Structured Programming
static • “static” is a special modifier • Usually variables and methods belongs to the class instance (object). Hence, objects are independent from each other • However, A variable or method that is defined static belongs to the class itself and shared by all its instances. • In this case if a static variable has been changed in any object this change will be reflected on all working objects. Structured Programming
static public class A { int x; static int y; } Structured Programming
static public class Main { public static void main(String[] args) { A a1 = new A(); a1.x = 5; a1.y = 10; A a2 = new A(); a2.x = 6; a2.y = 11; A a3 = new A(); a3.x = 7; a3.y = 12; System.out.println("a1.x : " +a1.x + "a1.y : " +a1.y); System.out.println("a2.x : " +a2.x + "a2.y : " +a2.y); System.out.println("a3.x : " +a3.x + "a3.y : " +a3.y); } } Output: a1.x : 5 - a1.y : 12 a2.x : 6 - a3.y : 12 a3.x : 7 - a3.y : 12 Structured Programming
static • In addition, a static variable can be reached directly from the class without the need of an instance (object) Structured Programming
static public class Main { public static void main(String[] args) { A a1 = new A(); a1.x = 5; A.y = 10; A a2 = new A(); a2.x = 6; A.y = 11; A a3 = new A(); a3.x = 7; A.y = 12; System.out.println("a1.x : " +a1.x + " - a1.y : " +A.y); System.out.println("a2.x : " +a2.x + " – a2.y : " +A.y); System.out.println("a3.x : " +a3.x + " - a3.y : " +A.y); } } Output: a1.x : 5 - a1.y : 12 a2.x : 6 - a3.y : 12 a3.x : 7 - a3.y : 12 Structured Programming
Constant • In java, a variable can be defined “final” which means that once it is initialized it cannot be changed • For example the following global variable is a constant: • public static final double pi=3.14; • Constants must be declared with static final and must have a n initial value at declaration which cannot be changed. • It may have any other modifier(public, private, protected or no modifier) Structured Programming
How objects are stored • When an object is created it will be stored in the computer memory • However, its name will contain only a reference to where it has been stored. • This different to the primitive data types (int, double, float, long, short, boolean and char) where the data itself is associated with the name. Structured Programming
How objects are stored memory public class A { int x; public A(int x) { this.x = x; } } a2 a1 i address data 1002 1001 20 1001 a1 data 1002 a2 data public class Main { public static void main(String[] args) { A a1 = new A(5); A a2 = new A(10); inti = 20; } } 1003 1004 1005 Structured Programming
How objects are stored • Because of this, programmers must be careful when creating, assigning and comparing objects as we will see in the following examples. Structured Programming
Assigning objects to objects public class A { int x; public A(int x) { this.x = x; } } memory a2 a1 address data 1002 1001 1001 a1 data 1002 a2 data public class Main { public static void main(String[] args) { A a1 = new A(5); A a2 = new A(10); a1.x = 15; a2.x = 20; System.out.println(a1.x); System.out.println(a2.x); } } 1003 1004 Output: 15 20 1005 Structured Programming
Assigning objects to objects public class A { int x; public A(int x) { this.x = x; } } memory a2 a1 address data 1001 1001 1001 a1 & a2 data 1002 public class Main { public static void main(String[] args) { A a1 = new A(5); A a2 = a1; a1.x = 15; a2.x = 20; System.out.println(a1.x); System.out.println(a2.x); } } 1003 1004 Output: 20 20 1005 Structured Programming
Assigning objects to objects • In the first example creating 2 different objects by calling the constructor of each one of them meant that they are completely independent of each other. • In the second example, however, by assigning the first object to the second we really copied the reference of the first object to the second which meant that any changes to either of them is reflected on the other because they are in fact the same. Structured Programming
Comparing objects public class A { int x; public A(int x) { this.x = x; } } memory a2 a1 address data 1002 1001 1001 a1 data 1002 a2 data public class Main { public static void main(String[] args) { A a1 = new A(5); A a2 = new A(5); if(a1 = = a2) System.out.println(“true”); else if(a1 != a2) System.out.println(“false”); } } 1003 1004 Output: false 1005 Structured Programming
Comparing objects public class A { int x; public A(int x) { this.x = x; } } memory a2 a1 address data 1001 1001 1001 a1 & a2 data 1002 public class Main { public static void main(String[] args) { A a1 = new A(5); A a2 = a1; a1.x = 10 if(a1 = = a2) System.out.println(“true”); else if(a1 != a2) System.out.println(“false”); } } 1003 1004 Output: true 1005 Structured Programming
Comparing objects • When comparing two objects using “= =” the compiler will compare if the reference of the two objects are the same regardless of the actual data inside it. • For that, the result of comparison of the first example was “false” because a1 and a2 have different references (1001 and 1002) • In the second example assigning a1 to a2 resulted in both objects having the same reference (and as a result the same data) which meant the result of the comparison is “true” Structured Programming
Passing parameters • Similarly, when an object is passed into a method as a parameter, its reference is sent which means that any change to the object in the called method will be reflected in the original object in the calling methods. • This is called “call by reference” while using primitive data types is called “call by value” because the actual value is sent to the called method and hence any changes does not affect the original variable Structured Programming
Passing parameters public class Main { public static void main(String[] args) { A a = new A(10); int y = 5; changeObject(a,y); System.out.println(a.x); System.out.println(y); } public static void changeObject(A a, int y){ a.x = 100; y = 50; } } Output: 100 5 Structured Programming