this keyword and final keyword
TRANSCRIPT
Introdraction of this keyword
• “ this “keyword is the refer to the
current object.
• This keyword is used to the pointout the
current object.
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.
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) ;
}
public static void main (String args [] )
{
student s1 = new student ( 1 ,” Kinjal ”);
Student s2 = new student ( 2 ,” Veena ”);
Student s3 = new student ( 6 ,” dhariya ”);
}
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) ;
}
Output:
1 : Kinjal
2 : veena
6 : dhariya
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
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.
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
Simple program of using the
final keyword
class abc
{
final void show ()
{
system . out .println (“Hi”) ;
}
}
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 () ;
}
}
Thank you