this keyword and final keyword

14

Upload: kinjalbirare

Post on 22-Jan-2018

133 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: This keyword and final  keyword
Page 2: This keyword and final  keyword

Introdraction of this keyword

• “ this “keyword is the refer to the

current object.

• This keyword is used to the pointout the

current object.

Page 3: This keyword and final  keyword

This keyword

The this keyword is refers to the current class

object.

The this keyword is use the point out the

current class object.

Java objects include a data member named

a THIS , which is actually a reference to the

current object.

The THIS keyword is useful if you need to

refer to the current object.

Page 4: This keyword and final  keyword

Simple example of This

keywordclass student

{

int r_no;

String name;

student (int r,String n)

{

this.r_no = r ;

this.name = n ;

System.out.println (r_no:+name) ;

}

Page 5: This keyword and final  keyword

public static void main (String args [] )

{

student s1 = new student ( 1 ,” Kinjal ”);

Student s2 = new student ( 2 ,” Veena ”);

Student s3 = new student ( 6 ,” dhariya ”);

}

Page 6: This keyword and final  keyword

Now here by using this keyword “THIS” like this.r_no we mean that variable r_no of the invoking object.

student s1 = new student ( 1 ,” Kinjal ”);

student ( int r , String n )

{

this . r_no = r ; //means s1.r_no

this . name = n ; // means s1.name

System . Out . println (r_no:+name) ;

}

Page 7: This keyword and final  keyword

Output:

1 : Kinjal

2 : veena

6 : dhariya

Page 8: This keyword and final  keyword

Final Keyword

Page 9: This keyword and final  keyword

final keyword Java’s FINAL keyword has slightly different

meanings depending on the context,but in general it say

“ This cannot be changed” or ”This is final”

Use of “final” in java is synonymous to use of “Const” in c++.

The following section discuss the three places where final can be used for:

Data

method

classes

Page 10: This keyword and final  keyword

final Data

Java provides you way to fix the value of any

variable by using the “final” keyword when

you declare it..

For example:

final int a = 100;

final int b=4;

The final keyword specifies that the value of a

variable is final and must not be change.

This makes it easy to see which variables are

defined as constant value.

Page 11: This keyword and final  keyword

final method and class

The final keyword use for more then one

purpose in java programing.

It can be userd with class and member.

When the final keyword used with a class,

that prevents the class from main

inherintance,you cannot inherit class

Page 12: This keyword and final  keyword

Simple program of using the

final keyword

class abc

{

final void show ()

{

system . out .println (“Hi”) ;

}

}

Page 13: This keyword and final  keyword

class abc1 extends abc

{

public void show ();

{ Error

system . out . Println (“ Hello”) ; Display

}

}

class test

{

public static void main (String args [ ] )

{

abc1 .a1 = new abc1 () ;

a1 . show () ;

}

}

Page 14: This keyword and final  keyword

Thank you