OOPs (this and static keywords)

                                       this keyword

What is this keyword?

  1. It is a predefined keyword supplied by java compiler (javac).
  2. this keyword is used to solve variable name conflicts.
  3. For JVM this keyword means current object address.
  4. For programmer this keyword can be used to refer instance variable.
  5. Never use this keyword in static methods. (it's not recommended)

What is the use of this keyword?

  1. this keyword is used to solve variable name conflicts.

    eg. public class A{
              public int i = 0;
              public A(int i )
             {
                  System.out.println(i); //here output will contain the value of local variable i.
                  System.out.println(this.i); // here value of instance variable i will be printed.
  2. this keyword can be used to call same class constructor, from other constructor.
  3. this keyword can also be used to call same class method. 

Rules of Compiler and JVM:-

Compiler Rule :- For every non-static method definition, compiler will add first parameter as this                                    keyword.

JVM Rule :- For every non-static method definition, JVM passes current object address as first                                 parameter.

eg. public class A{                                            public class MyProgram{
          public int i = 0;                                        public static void main(String[] args) 
          public A(A this,int i )                               {   
         {                                                                      int i =5;
                                                                                  A a1 = new A(0x100, i);
             -------CODE------
          }

In class A (A this) is added by compiler, while in class <MyProgram> some address of the current object of A i.e suppose 0x100 is added by JVM while executing the program.


Rules for using this() constructor call :-

  1. this() constructor call should always be the first line of the constructor.
  2. A normal method can't use this() to call constructor.
  3. Iterative constructor calls are not possible.

Note :-  It removes code duplicacy.


                          

                                      Static Keyword

What is static keyword?

  1.  It is a predefined keyword supplied by java compiler.
  2. It is used before variables and methods to make them static.

  3. static variables are related to class variables.(i.e they can be accessed by using class Name and . operator)
  4. static variables will be created only once at the time of class loading.
  5. static variables are stored in data segment.
  6. Just like we access static variables with class name, Similarly we can access static method with class Name.
  7. A static method can access only
    a) static variables and
    b)static methods of the same class.

Use of static variable and static method:-

  1. Static variables are used when we need to share a common data with different objects of the class. E.g. For a student class maintained for a particular college, the college name can be made static as it will remain same for every instance of student.
  2. Static methods are used to access static variables.  These are also used in Singleton Classes which I will cover later.

Static Initialization Block:-

A static block looks like this:- 

static
{
  ---------CODE--------
}

static block is executed only once i.e at class loading time.

Note:- While loading a class JVM follows a priority, which is given below:-
  1.    Static Variable
                 Static Block
                 Static Method 
             












Comments