Non Static Global Variable

 //Non Static Global Variable:-

  • // A global Variable Does not contain any Static keyword is called as Non Static Global Variable.
  • // It is also called Instance variable (instance-object).

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

//Program I:-

package variableStudy; 

public class NonStaticGlobalVariable

{      

//Calling Non Static Variable From Same Class:-

    //1.Non Static Global Variable

    int a=100; 

   public static void main(String[] args)

   {

     //3.create object of class

      NonStaticGlobalVariable n=new NonStaticGlobalVariable();

                    

     //4.Calling Non Static Variable From Same Class.

      System.out.println("Global Value of a is "+n.a);

                    

 //Calling Non Static Variable From Differant Class:-

     //5.need to create object of another class (DifferntClass).

       DifferantClass d=new DifferantClass();

                    

     //6.Calling Non Static Variable From Differant Class

       System.out.println("Global Value of p is "+d.p);

   }

      public void test()

          {

            //2.Non Static Method

          }            

}

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

//Prgram II:-(DifferantClass)

package variableStudy; 

public class DifferantClass

{

       //Non Static Global Variable

       int p=2222;

}

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

//Output:-

Global Value of a is 100

Global Value of p is 2222

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

Post a Comment

0 Comments