Lecture 04 Oop210
Lecture 04 Oop210
Ralph Tambala
MUST . CSIT
Lecture 4 1 / 18
Outline
1 this Keyword
2 final Keyword
3 static Keyword
Lecture 4 2 / 18
this Keyword
this Keyword
In Java, you will often encounter or use this this keyword. It is commonly
used as a reference variable that refers to the current object.
Lecture 4 3 / 18
this Keyword
this Keyword
this to refer instance variable
Accessing class level variable (instance variable) using this keyword is the
most common use of this keyword.
Syntax: this.variableName
1 class Student {
2 String name ;
3 public void setName ( String name ) {
4 this . name = name ; // name = name ; is wrong !
5 }
6 }
Here, this has been used in the event that we use same name for local
variable and instance variable. Otherwise, JVM will use local variable
always, and it will not set the value to instance variable.
Lecture 4 4 / 18
this Keyword
this Keyword
this to invoke instance method
this can be used for invoking instance methods within the same class.
Syntax: this.methodName
1 class Greeting {
2 public void hello () {
3 System . out . println ( " Muli uli ? " ) ;
4 }
5 public void greet ( String name ) {
6 this . hello () ;
7 hello () ; // optionally , works too without this
8 }
9 }
If one chooses to not use this keyword, JVM always calls the method
from same class by default.
Lecture 4 5 / 18
this Keyword
this Keyword
this to invoke current class constructor
this keyword can also be used to call constructor of same class. When we
have several constructors with different parameters, we can use the this
keyword to reuse an existing constructor.
Lecture 4 6 / 18
this Keyword
this Keyword
this to return current class instance
Lecture 4 7 / 18
this Keyword
this Keyword
this to pass as an argument in the method
Syntax: methodName(this)
1 class Student {
2 String name ;
3 void msg ( Student st ) {
4 System . out . println ( " Obj created ! " ) ;
5 }
6 void report () {
7 msg ( this ) ; // passing this as an argument
8 }
9 public static void main ( String [] args ) {
10 Student student = new Student () ;
11 student . report () ;
12 }
13 }
Lecture 4 8 / 18
this Keyword
this Keyword
this as an argument in the constructor call
final Keyword
final keyword can be used when declaring an entity. Using the final
keyword means that the value cannot be modified in the future.
Lecture 4 10 / 18
final Keyword
final Keyword
Declaration of Final Variables
1 public class A {}
1 public class B {
2 final private int a = 20; // ok
3 a = 30; // throws an error
4 final int b ; // blank final
5 B () {
6 b = 30; // ok , initialization inside constructor
7 }
8 final A myA = new A () ;
9 A theirA = new A () ;
10 myA = theirA ; // error , myA was declared as final
11 }
Lecture 4 11 / 18
final Keyword
final Keyword
Declaration of Final Methods
Basically, without final keywords the example above would not produce an
error. We will discuss in detail what method overriding and keyword
extends work when we look at Polymorphism.
Lecture 4 12 / 18
final Keyword
final Keyword
Declaration of Final Classes
Lecture 4 13 / 18
final Keyword
final Keyword
Important Notes
Here are some important points to take note of when using the final
keyword:
1 A class constructor cannot be final.
2 A block cannot be final.
3 A local final variable must be initialized at the time of declaration.
4 We cannot change the value of a final variable after initialization.
5 A final method cannot be overridden.
6 A final class cannot be inherited.
7 We can create the object for a final class but cannot extend it.
8 If the method parameters are declared as final, their values
(arguments) cannot be changed.
.
Lecture 4 14 / 18
static Keyword
static Keyword
If a field (class, block, method or variable) is declared static, then exactly
a single copy of that field is created and shared among all instances of that
class.
Static members are common for all the instances(objects) of the class but
non-static members are separate for each instance of class.
1 class Demo {
2 static int num ;
3 static void work () { // static method
4 System . out . println ( " Some task eh ! " ) ;
5 }
6 static { // a static block
7 counter = 11;
8 }
9 public static void main ( String [] args ) {
10 work () ; // called without creating object
11 }
12 }
Lecture 4 15 / 18
static Keyword
Imagine you want to generate an id for each who registers on your website
dynamically. A first user to sign up get id = 1, second user gets id = 2,
and so on.
Basically, we can implement the code as follows:
1 class User {
2 static int counter = 0;
3 int id ;
4 User () {
5 id = counter ++;
6 }
7 public static void main ( String [] args ) {
8 User user1 = new User () ;
9 User user2 = new User () ;
10 System . out . println ( " user1 ID = " + user1 . id + " \ n " +
11 " user2 ID = " + user2 . id ) ;
12 }
13 }
Lecture 4 16 / 18
static Keyword
practice
Lecture 4 18 / 18