Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Wjdan Ramadan
Software Engineer
Fayez Ghazzawi & Graduate of Faculty of Information
Software Engineer Technology Engineering
10
Fayez Ghazzawi
Wjdan Ramadan
Methods Main Tips
Fayez Ghazzawi
Wjdan Ramadan
Instance vs. Class Methods
• Class Methods are shared by all instances, but Instance
Methods are called from each object separately.
• Class Methods are declared as static, Instance Methods are
non-static.
• Class Methods are called by using the name of the class or the
name of object, but Instance Methods can only be called by the
name of object.
Fayez Ghazzawi
Wjdan Ramadan
Example
public class myClass
{
public void Hi() { System.out.println(“Hi”); }
Hi
Compile Error !!!
Fayez Ghazzawi
Wjdan Ramadan
Example
public class myClass
{
public static void Hi() { System.out.println(“Hi”); }
Hi
Hi
Fayez Ghazzawi
Wjdan Ramadan
Method Overloading
• Method overloading is essential developed to allow the same
method name to be used with different argument types.
• We can overload a method by creating another one in the same
class with same name but different number or types of
parameters.
• Overloaded Methods can’t have the same Signature.
• Remember !!! Return Type of a method is not considered as a
part of the method’s signature so we can’t distinguish the
overloaded methods by its return type.
Fayez Ghazzawi
Wjdan Ramadan
Example
Fayez Ghazzawi
Wjdan Ramadan
this Reference
• this is a reference which refers to the current instance of the
class.
• It is usually used with Setters, Getters and Constructors.
• It is use to reach instance variables or methods.
• But any class variables or methods (variables and methods
which are declared as static) can be reached by this
reference.
Fayez Ghazzawi
Wjdan Ramadan
Example
public class Student
{
private int num;
private String name;
Fayez Ghazzawi
Wjdan Ramadan
final Keyword
• final Variable is a constant that can’t be modified.
• If final is used within a primitive the value is constant but if it’s
used within an object the reference is constant.
• Therefore final variable must be initialized.
• final Method is a method that can’t be overridden by a child
of its class.
• final Class is a class that can’t be inherited from.
Fayez Ghazzawi
Wjdan Ramadan
final Variables
• final variables has two types:
1. Initialized final:
this final variable is initialized when declared.
2. Blank final:
this final variable is initialized in the class’s constructors.
• final variable must be initialized. if not, a Compile
Error is occurred.
Fayez Ghazzawi
Wjdan Ramadan
Example
public class One
{
public final int i= 0;
Fayez Ghazzawi
Wjdan Ramadan
final Methods
Fayez Ghazzawi
Wjdan Ramadan
final Classes
• Final classes can’t be inherited from.
• Any variable in a final class can be final or not
according to the programmer’s decision.
• All methods in a final class are implicitly final because
the final class can’t be inherited from so all the methods
can’t be overridden.
Fayez Ghazzawi
Wjdan Ramadan
Setters and Getters
Fayez Ghazzawi
Wjdan Ramadan
Example
public class Student
{
private int num; private String name;
Fayez Ghazzawi
Wjdan Ramadan
Constructors
• Constructor are used for initializations.
• It is a method that is called automatically when an object is
created.
• The Constructor consists:
1. Having the same name of the class.
2. Having no return type.
3. Being declared as public (optional).
• The Compiler creates a no-argument constructor if there are no
constructors explicitly provided.
Fayez Ghazzawi
Wjdan Ramadan
Default Constructor
• It is a constructor without arguments.
• The compiler creates a default constructor if there is no
constructor specified explicitly.
• If there is a specified constructor, the compiler won’t create one
for you :P .
Fayez Ghazzawi
Wjdan Ramadan
Example
public class Student
{
private int num; private String name;
Fayez Ghazzawi
Wjdan Ramadan
Example
public class Student
{
private int num; private String name;
Fayez Ghazzawi
Wjdan Ramadan
Overloading Constructors
• Because constructors must have the same name as the class
itself, we can specify several constructors with different
arguments.
public class Student
{
private int num; private String name;
public Student()
{ this.num = 0; this.name = “ “; }
Fayez Ghazzawi
Wjdan Ramadan
Example
public class Student
{
private int num; private String name;
public Student()
{ this(0, “ “); }
Fayez Ghazzawi
Wjdan Ramadan
Static Initializer Block
• Static Initializer Block is a block that:
1.Not named.
2.Has no return type.
3.Begins with static keyword.
• It is similar to the constructor, but it is used to initialize all class
(static) variables previously declared.
• It is executed first, only once and for all objects.
Fayez Ghazzawi
Wjdan Ramadan
Example
public class Car
{
private static int count;
static{
count = 50;
}
51
Fayez Ghazzawi
Wjdan Ramadan
The End.
See you next class
https://www.facebook.com/fayez.ghazzawi.3.4
fayezghazzawi@hotmail.com
Fayez Ghazzawi
Wjdan Ramadan