3. *It is refer to the object that is currently executed.
*It initializes the value of a variable
*Syntax = this.variable name;
4. *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
5. *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.
7. *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
8. *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
9. *
*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
10. * 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);
* }
* }
11. *class boxprise extends box
*{
* int prise;
* boxprise(int w, int h, int d, int p)
* {
* super(w,h,d);
* prise = p;
* }
*}
12. *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.
13. * 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);
* }
* }
14. * 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
15. *
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.
16. *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");
* }
* }
*
17. *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.