Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Programming 1401104-3 Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.

Similar presentations


Presentation on theme: "Structured Programming 1401104-3 Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming."— Presentation transcript:

1 Structured Programming 1401104-3 Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming

2 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 Programming2

3 Packages Lets assume the following example where the classes have the following hierarchy: Structured Programming3

4 Packages In the previous example, Class1 can be used directly while Class2 and Class3 must be imported Structured Programming4

5 Modifiers Modifiers are keywords used before the declaration of a variable or method to control who can see and use it For example : o public int x; o protected double y; o Public void print(){…} Structured Programming5

6 Modifiers There are 4 modifiers (private, protected, public and using no modifiers) The next table shows the visibility of each modifier. Structured Programming6 ModifierClassPackageSubclassWorld publicYYYY protectedYYYN no modifierYYNN privateYNNN

7 Modifiers Lets assume the following example Structured Programming7

8 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 Programming8 ModifierAlphaBetaAlphasubGamma publicYYYY protectedYYYN no modifierYYNN privateYNNN

9 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 Programming9

10 static public class A { int x; static int y; } Structured Programming10

11 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); } Structured Programming11 Output: a1.x : 5 - a1.y : 12 a2.x : 6 - a3.y : 12 a3.x : 7 - a3.y : 12

12 static In addition, a static variable can be reached directly from the class without the need of an instance (object) Structured Programming12

13 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); } Structured Programming13 Output: a1.x : 5 - a1.y : 12 a2.x : 6 - a3.y : 12 a3.x : 7 - a3.y : 12

14 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: o 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 Programming14

15 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 Programming15

16 How objects are stored Structured Programming16 public class A { int x; public A(int x) { this.x = x; } a1 data a2 data 1001 1002 1003 1004 1005 memory dataaddress 1001 a1 1002 a2 public class Main { public static void main(String[] args) { A a1 = new A(5); A a2 = new A(10); int i = 20; } 20 i

17 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 Programming17

18 Assigning objects to objects Structured Programming18 public class A { int x; public A(int x) { this.x = x; } a1 data a2 data 1001 1002 1003 1004 1005 memory dataaddress 1001 a1 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); } 1002 a2 Output: 15 20

19 Assigning objects to objects Structured Programming19 public class A { int x; public A(int x) { this.x = x; } a1 & a2 data 1001 1002 1003 1004 1005 memory dataaddress 1001 a1 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); } 1001 a2 Output: 20

20 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 Programming20

21 Comparing objects Structured Programming21 public class A { int x; public A(int x) { this.x = x; } a1 data a2 data 1001 1002 1003 1004 1005 memory dataaddress 1001 a1 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”); } 1002 a2 Output: false

22 Comparing objects Structured Programming22 public class A { int x; public A(int x) { this.x = x; } a1 & a2 data 1001 1002 1003 1004 1005 memory dataaddress 1001 a1 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”); } 1001 a2 Output: true

23 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 Programming23

24 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 Programming24

25 Passing parameters Structured Programming25 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


Download ppt "Structured Programming 1401104-3 Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming."

Similar presentations


Ads by Google