*Actual way of calling static members (variables and methods) is
className.staticMemberName.

ex-
1)
public class StaticMembers {
    public static void main(String[] args) {
        System.out.println(“## program starts ##”);
        System.out.println(“Addition of two numbers “+StaticMembers.add());
        System.out.println(“## program ends ##”);
    }
    public static int add(){
        int a = 5;
        int b = 6;
        int c = a+b;
        return c;
    }
}

output-
## program starts ##
Addition of two numbers 11
## program ends ##

2)
public class StaticMembers {
    static double a = 34.5;
    public static void main(String[] args) {
        double a = 43.6;
        System.out.println(“program starts”);
        System.out.println(“local variable a- “+a);
        System.out.println(“global variable a- “+StaticMembers.a);
        StaticMembers.staticMethod();
        System.out.println(“Program ends.”);
    }
    public static void staticMethod(){
        System.out.println(“static method.”);
    }
}

output-
program starts
local variable a- 43.6
global variable a- 34.5
static method.
Program ends.

==========================================================================
**Convention for method name-
*1st letter should be small of 1st word then the 1st letter of the rest of the word should be caps.


Assignment- 

1) write a program to calculate simple interest.
2) write a program to calculate volume of sphere.

*Final Variables- variables which needs to be initialize at the time of declaration and which can not be reinitialize.
ex- final static double pi = 3.14;

Ex-
public class Circle {
    final static double pi = 3.14;
    public static void main(String[] args) {
        System.out.println(“program starts”);
        Circle.areaOfCircle(2);
        System.out.println(“Program ends.”);
    }
    public static void areaOfCircle(double r){
        double area = pi*r*r;
        System.out.println(“area of cicle -> “+area);
    }
}

Note-  

  • Final means no more changes. 
  • Local variables can also be final variables.
  • Final variables must be initialized else it will give compilation error.

==========================================================================
**Non Static Members (variables and methods)
       If a global variable or method is not declared as static then that variable or method is non-static.

*How to create an Object-
        ClassName     refVarName     =     new     ClassName();

*How to call non-static members
       Create an object and then use the reference variable to call the non-static member.

      
         ClassName     refVarName     =     new     ClassName();
         refVarName.nonStaticMemberName;

ex- 
public class NonStaticMembers {
    int nonStaticVar;
    public static void main(String[] args) {
        System.out.println(“program starts”);
       
        NonStaticMembers refVarName = new NonStaticMembers();  //this is creating the object
        System.out.println(refVarName.nonStaticVar); //this is calling non-static variable , this will print 0 because 
        //if global variable is not initialize then compiler will by default initialize it with default value
        refVarName.nonStaticMethod();  //this is how to call the non-static method
        System.out.println(“Program ends.”);
    }
    public void nonStaticMethod(){
        System.out.println(“Inside non-static method.”);
    }
}
 
output-program starts
0
Inside non-static method.
Program ends.


===================================================
Class and Object- 
     Class is a template.
     Object is a mirror image of the class. We can create ‘n’ number of the objects.

ex-
 public class NonStaticMembers {
    int nonStaticVar;
    public static void main(String[] args) {
        System.out.println(“program starts”);
        
        NonStaticMembers refVarName1 = new NonStaticMembers();  //this is creating the object1
        NonStaticMembers refVarName2 = new NonStaticMembers();  //this is creating the object2
        System.out.println(refVarName1.nonStaticVar);
        System.out.println(refVarName2.nonStaticVar);
        System.out.println(“Program ends.”);
    }
}

 output-
program starts
0
0
Program ends. 

 

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