Keywords of java

jani99harsh 1,379 views 18 slides Apr 20, 2015
Slide 1
Slide 1 of 18
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18

About This Presentation

it explain keywords of java with example..


Slide Content

Keywords of Java By Harsh Jani [email protected]

Content This keyword Static keyword   Super keyword   Final keyword

This keyword It is refer to the object that is currently executed. It initializes the value of a variable Syntax = this.variable name;

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

Static keyword 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.

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; } }

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

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

Super keyword 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  

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); } }

class boxprise extends box { int prise ; boxprise ( int w, int h, int d, int p) { super( w,h,d ); prise = p; } }

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.

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); } }

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

Final keyword 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.

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"); } }  

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.