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:
Programs:
- class P6
- {
- static int a;
- public static void main(String [ ] args)
- {
- System.out.println(a);
- }
- }
Output:
0
xxx------xxxx------xxxx-----xxxx----xxxx----xxx---xx--xxx---xxxx----xxxx------xxxx---------xxx
- class P7
- {
- static int a=35; //Global var
- public static void main(String [ ] args) //static context
- {
- System.out.println(a); //35
- }
- }
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:
- All the method blocks will be stored inside method area.
- Address of static methods will be stored inside class static area.
- static method is also known as static context.
- Every static context will be pointing towards class static area.
- class static area is used to store static members.
- 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
- class P7
- {
- static int a=35;
- public static void main(String [ ] args)
- {
- int a =40;
- System.out.println(a);
- System.out.println(P7.a);
- }
- }
Output:
40
35
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:
- static block doesn't have any name.
- static block doesn't have any formal arguments.
- static block doesn't have any return type.
- static block executed automatically (Implicity) by the compiler.
- 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
- class P8
- {
- public static void main(String [ ] args)
- {
- System.out.println("main page is displaying");
- }
- static
- {
- System.out.println("Enter ID");
- System.out.println("Enter Password");
- }
- }
Output:
Enter ID
Enter Password
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
- class P9
- {
- static double a;
- static char b;
- public static void test( )
- {
- System.out.println("Test B");
- System.out.println(a);
- System.out.println(b);
- a=25.9;
- b='a'
- System.out.println("Test E");
- }
- static
- {
- System.out.println("SIB-1");
- }
- public static void main(String [ ] args)
- {
- System.out.println("MB");
- test( );
- System.out.println(a);
- System.out.println(b);
- System.out.println("ME");
- }
- static
- {
- System.out.println("SIB-2");
- }
- }
Output:
SIB-1
SIB-2
SIB-2
MB
Test B
Test B
0.0
Test E
25.9
a
ME
xxx------xxxx------xxxx-----xxxx----xxxx----xxx---xx--xxx---xxxx----xxxx------xxxx---------xxx
0 Comments