keywords of java
TRANSCRIPT
![Page 2: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/2.jpg)
Content
![Page 3: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/3.jpg)
*It is refer to the object that is currently executed.
*It initializes the value of a variable
*Syntax = this.variable name;
![Page 4: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/4.jpg)
*Example* class xyz
* { int x;
* int y;
* xyz(int x, int y)
* {
* //if instance variable & parameter variable are different than no need of this keyword.
* this.x=x;
* this.y=y;
* }
* }
* class demo
* { public static void main(String args[])
* {
* xyz ob = new xyz(1,2);
* System.out.println(ob.x);
* System.out.println(ob.y);
** }
* }
* Output: 1
* 2
![Page 5: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/5.jpg)
*Normally any of the class members can be accessed by using
object of its class, but it is possible to create a member that
can be used by itself without reference to a specific instance.
*It is possible by a static keyword.
*You can declare both method and variable to be static.
*If methods are declared static then
They can call other static methods.
They must only access static data.
They cannot refer to this or super keyword in any way.
*It provides static block.
![Page 6: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/6.jpg)
*Example:
**class abc
* {
* static int a=10;
* static int b;
* // static variables
** static void printAll(int n)
* {
* System.out.println("N = "+n);
* System.out.println("A = "+a);
* System.out.println("B = "+b);
* }
* static
* {
* System.out.println("Static block executed");
* b=a*2;
* }
* }
![Page 7: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/7.jpg)
*class demo
* {
* public static void main(String args[])
* {
* // static methods are directly called without object
** printAll(5);
* }
* }
*Output: N = 5
* A = 10
* Static block executed
* B = 20
![Page 8: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/8.jpg)
*Example – 2
* class teststatic
* {
* static int a = 10;
* static int b = 20;
* // static variables
* static void printA()
* {
* System.out.println("A = "+a);
* }
* }
* class staticinotherclass
* {
* public static void main(String args[])
* {
* // static methods are directly called without object
** teststatic.printA();
* System.out.println("B from other class = "+teststatic.b);
* }
* }
* Output: A = 10
* B from other class = 20
![Page 9: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/9.jpg)
*
*Sometime we may wish to use superclass constructor
as they were implemented or we may wish to refer
super class variable into subclass where variables
with the same name as in superclass exists then java
provides a keyword super to solve above difficulties.
*You may use super keyword for
To call superclass constructor into a subclass
To refer superclass variable
![Page 10: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/10.jpg)
* Example: To call superclass constructor into a subclass
* class box
* {
* int width,height,depth;
* // used when no parameter given
* box()
* {
* width=height=depth=10;
* }
* box(int a)
* {
* width=height=depth=a;
* }
* box(int w, int h, int d)
* {
* width=w;
* height=h;
* depth=d;
* }
* int volumeofbox
* {
* return(width*depth*height);
* }
* }
![Page 11: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/11.jpg)
*class boxprise extends box
*{* int prise;
* boxprise(int w, int h, int d, int p)
* {
* super(w,h,d);
* prise = p;
* }
*}
![Page 12: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/12.jpg)
*class demo
* {
* public static void main(String args[])
* {
* box b1 = new box();
* System.out.println("Box1 volume is = "+(b1.volumeofbox));
* box b2 = new box(20);
* System.out.println("Box2 volume is = "+(b2.volumeofbox));
* boxprise b3 = new boxprise(3,4,5,600);
* System.out.println("Price of box is = "+(b3.priseofbox));
* System.out.println("Box3 volume is = "+(b3.volumeofbox));
* }
** }
*Output: Box1 volume is = 1000
Box2 volume is = 6000
Price of box is = 600
Box3 volume is = 60
*When we use super to call superclass constructor then super(); must be in the first line of subclass.
![Page 13: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/13.jpg)
* Example: To refer superclass variable
* class A
* {
* int a;
* }
* class B extends A
* {
* int a; int b;
* B(int x, int y, int z)
* {
* super.a=x;
* // assigns value to a variable of super class
* a=y;
* b=z;
* }
* void displayAll()
* {
* System.out.println("super.a =", +super.a );
* // access variable of super class in sub class
* System.out.println("A = ",+a);
* System.out.println("B = ",+b);
* }
* }
![Page 14: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/14.jpg)
* class demo
* {
* public static void main(String args[])
* {
* B b1 = new B(10,20,30);
* b1.displayAll();
* }
* }
* Output: super.a = 10
* A = 20
* B = 30
![Page 15: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/15.jpg)
*
To declared as constant
*Whenever you want to declare any variable whose
value cannot be changed at any time then you can do
this by declaring that variable as final.
*Using final you can define constant in java program
*Syntax = final datatype variablename = value;
*Example = final float PI = 3.14;
*Generally variable declared as final which are written
in uppercase.
![Page 16: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/16.jpg)
*To prevent method overriding
*Write the final precede the method than it cannot be overridden.
* Method declared as final cannot be declared as an abstract method because it cannot be overridden.
*Example:
* class xyz
* {
* final void printmessage()
* {
* System.out.println("hello");
* }
* }
* class B extends xyz
* {
* final void printmessage()
* {
* // error in the following line because method declared as final cannot be override
* System.out.println("hello");
* }
* }
*
![Page 17: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/17.jpg)
*To prevent inheritance
*To do this write final precede the class declaration than it cannot be inherited.
*Declaring class as final, indirectly declared all of its method as final too.
*It is illegal to declare a class as both abstract and final.
*Example:
*final class A
*{*}*class B extends A // illegal
*{*}*A final class must full defined its methods with complete definition
because they are directly final and final method cannot be redefine.
![Page 18: Keywords of java](https://reader031.vdocuments.us/reader031/viewer/2022020218/55a941a41a28abfd5c8b46bc/html5/thumbnails/18.jpg)