Explanation of Java class execution-

When java class is executed, the following steps will happen-

  1. Memory gets allocated for the execution.
  2. The allocated memory is divided into stack area and heap area.
  3. stack is an implementation of first in and 1st out, which is used for execution purpose.
  4. Heap area is a random storage area which is used to store the information of program.
  5. Java or JVM enters the stack area for its execution.
  6. Java calls class loader program to load all the members of class.
  7. The class loader program loads only the static members of the program into a specific area on the heap called Static Pool.
  8. Now Java calls main method to stack area for its execution, the main method starts executing each statement one by one.
  9. When java or jvm comes across instance or object creation statement, the object is created in the heap area and object address is stored in the reference variable.
  10. At the time of object/instance creation, all the non-static members of the class are loaded into object and address of that object is saved into reference variable.
  11. Java before exiting the stack area, it calls Garbage collector which frees cleans the memory in the heap area.

Note-

  • In entire program, only one copy of the static member exist.
  • Multiple copies of non-static members can exist, which depends on number of times the object is created.
  • Static members are loaded at the time of class loading.
  • non-static members are loaded at the time of instance(Object) creation.
  • Static members(method or var) can be directly access in static method as well as non-static method.
  • Non-static members of the class can be directly access only in non-static methods.
  • To access non-static members, inside static method, an object of the class has to be created. Then use the reference variable to access non-static members.
  • An Object whose ref address is lost know as Abundant Object. To free the memory of abundant object before program termination, the programmer has to call the garbage collector explicitly.

Example-
public class Demo {
    int nonStaticVar=10; //this needs to called by ref var
    static String staticVar = “static variable, this can be called directly”;
    public static void main(String[] args) {
        System.out.println(“program starts”);
        Demo refVarName = new Demo();  //this is creating the object
        
        System.out.println(refVarName.nonStaticVar);
        System.out.println(staticVar);
        
        refVarName.nonStaticMethod();
        staticMethod();
        
        System.out.println(“Program ends.”);
    }
    public static void staticMethod(){
        System.out.println(“static method, this can be called directly.”);
    }
    public void nonStaticMethod(){
        System.out.println(“non-static method, this need to call by ref var”);
    }
}

output
program starts
10
static variable, this can be called directly
non-static method, this need to call by ref var
static method, this can be called directly.
Program ends. 

=================================================== 
very basics information (Though it’s very basic but trust me people not able to ans when asked in interview 😛 ;))-Arithmetic Operators-

===============

Operator          Result                   —————-            ———–
+                        Addition
–                          Subtraction
*                         Multiplication
/                         Division
%                       Modulus
++                     Increment
—                        Decrement
+=                     Addition Assignment
-=                       Subtraction Assignment
*=                      Multiplication Assignment
/=                       division Assignment
%=                     modulus Assignment.Relational Operators-
===============
Operator          Result                   —————-            ———–
==                     Equal to
!=                      Not equal to
>                       Greater than 
<                       Less than
>=                    Greater than or equal to
<=                    less than or equal to

AND and OR condtions-
——————————

syntax for AND condition-
———————————
 &
&& (this is short and)

explanation – 
if (x>0 && y>0)    —> in this case here if x<= 0 then it will not go to check for the other condition y>0.

but if (x>0 & y>0) —> in this case even if x<=0 then also it will go to check for the other condition y>0.

syntax for OR condition-
———————————
|
|| (this is short or)

explanation – 
if (x>0 || y>0)    —> in this case here if x>0 then it will not go to check for the other condition y>0.

but if (x>0 | y>0) —> in this case even if x>0 then also it will go to check for the other condition y>0.

===================================================

 

 

Source: http://selenium-makeiteasy.blogspot.com