Upload
indira-eaton
View
27
Download
0
Tags:
Embed Size (px)
DESCRIPTION
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) - PowerPoint PPT Presentation
Citation preview
1Structured Programming
Structured Programming
1401104-3
Dr. Atif Alhejali
Lecture 4
ModifiersParameters passing
Structured 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 Programming 3
Packages• Lets assume the following example where the
classes have the following hierarchy:
Structured Programming 4
Packages• In the previous example, Class1 can be used
directly while Class2 and Class3 must be imported
Structured Programming 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 Programming 6
Modifiers• There are 4 modifiers (private, protected, public
and using no modifiers)• The next table shows the visibility of each
modifier.
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
Structured Programming 7
Modifiers• Lets assume the following example
Structured Programming 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
Modifier Alpha Beta Alphasub Gamma
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
Structured Programming 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 Programming 10
staticpublic class A { int x; static int y; }
Structured Programming 11
staticpublic 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 : 12a2.x : 6 - a3.y : 12a3.x : 7 - a3.y : 12
Structured Programming 12
static• In addition, a static variable can be
reached directly from the class without the need of an instance (object)
Structured Programming 13
staticpublic 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 : 12a2.x : 6 - a3.y : 12a3.x : 7 - a3.y : 12
Structured Programming 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 Programming 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 Programming 16
How objects are stored
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
a2public class Main { public static void main(String[] args) { A a1 = new A(5); A a2 = new A(10);
int i = 20; }}
20
i
Structured Programming 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 Programming 18
Assigning objects to objects
public class A { int x; public A(int x) { this.x = x; }}
a1 data
a2 data
1001
1002
1003
1004
1005
memorydataaddress
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:1520
Structured Programming 19
Assigning objects to objects
public class A { int x; public A(int x) { this.x = x; }}
a1 & a2 data
1001
1002
1003
1004
1005
memorydataaddress
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:2020
Structured Programming 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 Programming 21
Comparing objectspublic class A { int x; public A(int x) { this.x = x; }}
a1 data
a2 data
1001
1002
1003
1004
1005
memorydataaddress
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
Structured Programming 22
Comparing objectspublic class A { int x; public A(int x) { this.x = x; }}
a1 & a2 data
1001
1002
1003
1004
1005
memorydataaddress
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
Structured Programming 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 Programming 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 Programming 25
Passing parameterspublic 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:1005