non static Members:     
Any member declared inside global area not prefix with static keyword known as non static member.


  1. class P1
  2. {   
  3. public void test( )
  4. {
  5.   
  6. }
  7.   
  8. }
 

non static Variables:
Any global variable not prefix with static keyword is known as non static variable.



How many ways we can used static members inside context  
2 ways      > 1) directly                    2) with the help of classname         

Program: 

  1. class P1
  2. {   
  3. static int  a=10;
  4. public void test( )
  5. public static void main(String [ ] args)
  6. {
  7. System.out.println("directly : "+a);
  8. System.out.println("Classname  : "+P1.a);
  9. }
  10.   
  11. }

Note: 
1]We can not used nonstatic members inside static context.
      1) Directly                   2) With the help of classname 
2]Non static members are stored inside object which is why programmer has to create object. 



How to create object ?
>>>By using new keyword


Syntax                new classname( );   

new: 
  1. new is a keyword.
  2. new is an unary operator.
  3. new operator creates a block of memory inside heap area during run time.
  4. new operator returns address of an object which is created.

Note: 
  • For one class we can create n-number of objects.
  • Whenever we write new keyword new object will be created.

Program: 

  1. class Mob
  2. {   
  3. public static void main(String [ ] args)
  4. {
  5. new Mob( );
  6. System.out.println(Mob ( ) );
  7. System.out.println(Mob ( ) );
  8. System.out.println(Mob ( ) );
  9. }
  10.  }
Output:
Mob@1                                                          
Mob@2                                                          
Mob@3                                                          
                                                                       



How to store and print address: 

  1. class Mob
  2. {   
  3. public static void main(String [ ] args)
  4. {
  5. Mob xyz = new Mob( );
  6. System.out.println(xyz );
  7. System.out.println(xyz );
  8. System.out.println(xyz );
  9. }
  10.  }
Output:
Mob@1                                                          
Mob@1                                                          
Mob@1                                                          
                                                                       

How to use nonstatic member inside static context
>>>>>>with the help of object reference               

Program:  
  1. class Mob
  2. {   
  3. int a = 20;
  4. public static void main(String [ ] args)
  5. {
  6. Mob xyz = new Mob( );
  7. System.out.println(xyz.a);
  8. }
  9. }
Output:
20                                                                   
                                                                       
                                                                       
                                                                       

How many times can we used static members inside static context
>>>>>>1)directly   2)help of classname  3)help of object reference               


Program:  

  1. class Mob
  2. {   
  3. static int a = 20;
  4. public static void main(String [ ] args)
  5. {
  6. Mob xyz = new Mob( );
  7. System.out.println(a);
  8. System.out.println(Mob.a);
  9. System.out.println(xyz.a);
  10. }
  11. }
Output:
20                                                                   
20                                                                   
20