Fundamentals of OOPS concept





Global Members
Any member declared inside global area is known as global member.



Static variable:-
Any global variable prefix with static keyword is known as static variable.

Static member:- 
Any member prefix with static keyword is known as static member.



Difference between Global variable and local Variable:

Sr. No

Global variable

Local Variable

1.

Variable declared inside global area is called as global variable

Variable declared inside a local area is called local variable.

2.

Global variable have its default value 

Local variables do not have any default values.

3.

Global variable can be used in global area as well as in local area.

Local variable can be used only in local area

4.

We can use variable without initialization.

We can not use variables without initialization in the local area.

5.

Global static variables can be stored inside a static class area.

Local variable stored inside in stack area.




Programs:

  1. class P6
  2. { 
  3. static int a;
  4. public static void main(String [ ] args)                                                
  5. {
  6. System.out.println(a);
  7. }

Output:

0                                                            
                                                              
                                                              
                                                              
                                                              

xxx------xxxx------xxxx-----xxxx----xxxx----xxx---xx--xxx---xxxx----xxxx------xxxx---------xxx


  1. class P7
  2. { 
  3. static int a=35;                                                      //Global var
  4. public static void main(String [ ] args)               //static context                                          
  5. {
  6. System.out.println(a);                                          //35
  7. }

Output:

35                                                          
                                                              
                                                              
                                                              
                                                              

xxx------xxxx------xxxx-----xxxx----xxxx----xxx---xx--xxx---xxxx----xxxx------xxxx-------


Static method: 
Any method declared inside global area prefix with static keyword is known as static method.

Note: 
  1. All the method blocks will be stored inside method area.
  2. Address of static methods will be stored inside class static area.
  3. static method is also known as static context.
  4. Every static context will be pointing towards class static area.
  5. class static area is used to store static members.
  6. We can create local variable and global variable with same name.
    • If we used directly high priority given to local variable.
    • To use static variable we have to consider classname.

Syntax                classname.varname;   

Program

  1. class P7
  2. { 
  3. static int a=35;                                                      
  4. public static void main(String [ ] args)                                           
  5. {
  6. int a =40;
  7. System.out.println(a);                                         
  8. System.out.println(P7.a);
  9. }

Output:

40                                                          
35                                                          
                                                              
                                                              

xxx------xxxx------xxxx-----xxxx----xxxx----xxx---xx--xxx---xxxx----xxxx------xxxx---------xxx

static block:
Any block declared inside global area prefix with static keyword is known as static block.


Characteristics of static block:
  1. static block doesn't have any name.
  2. static block doesn't have any formal arguments.
  3. static block doesn't have any return type.
  4. static block executed automatically (Implicity) by the compiler.
  5. Programmer can not call static block.
Why do we need static block ?
To execute some statements before execution of main method, but only once.

NOTE:
If we have multiple static block they get executed from top to bottom order.

Program

  1. class P8
  2. {                                                       
  3. public static void main(String [ ] args)                                           
  4. {
  5. System.out.println("main page is displaying");
  6. }
  7. static 
  8.                                        
  9. System.out.println("Enter ID");
  10. System.out.println("Enter Password");
  11. }

Output:

Enter ID                                                
Enter Password                                     
main page is displaying                        
                                                              

Difference between static method and static block
1]Any method prefix with static keyword is called as static method.
1) The block start with static keyword is known as static block.

2]We can execute static method n-number of times.
2)We can execute static block only once.

3]Programmer can call static method.
3)Compiler can call static block automatically.

4]static method have name, formal arguments and return type.
4)static block doesn't have any name, formal arguments and return type.

5]static method execute after static block.
5)static block execute before any method.

xxx------xxxx------xxxx-----xxxx----xxxx----xxx---xx--xxx---xxxx----xxxx------xxxx---------xxx

Program

  1. class P9
  2. {   
  3. static double a; 
  4. static char b; 
  5. public static void test( )
  6. {
  7. System.out.println("Test B");
  8. System.out.println(a);
  9. System.out.println(b);
  10. a=25.9;
  11. b='a
  12. System.out.println("Test E");
  13. }
  14. static 
  15. {
  16. System.out.println("SIB-1");
  17. }
  18. public static void main(String [ ] args)                                           
  19. {
  20. System.out.println("MB");
  21. test( ); 
  22. System.out.println(a);
  23. System.out.println(b);
  24. System.out.println("ME");
  25. }
  26. static 
  27. {                                         
  28. System.out.println("SIB-2");
  29. }

Output:

SIB-1                                                    
SIB-2                                                    
MB                                                       
Test B                                                   
0.0                                                         
                                                              
Test E                                                    
25.9                                                       
a                                                            
ME                                                        
                                                              

  xxx------xxxx------xxxx-----xxxx----xxxx----xxx---xx--xxx---xxxx----xxxx------xxxx---------xxx