Difference between Static and Non-Static fields of a class.

Difference between Static and Non-Static fields of a class.

Static variables or fields belong to the class, and not to any object of the class. A static variable is initialized when the class is loaded at runtime. Non-static fields are instance fields of an object. They can only be accessed or invoked through an object reference. The value of static variable remains constant throughout the class. The value of Non-static variables changes as the objects has their own copy of these variables.

Example of static and non-static fields:
class a

{

   static int n1=5, n2=6;

   static

   {

       System.out.println("A class static block");

   }

   static void m()

   {

       System.out.println("A class method");

   }

}

class b

{

   int n3=50;

   char n4='a';

   void m1()

   {

       System.out.println("B class method");

   }

   public static void main(String args[])

   {
       // static variables are accessed directly by giving the class reference in other class

       System.out.println(a.n1);

       System.out.println(a.n2);

       a.m();

       b obj=new b();//non-static variables are accessed with the help of class object

       System.out.println(obj.n3);

       System.out.println(obj.n4);

       obj.m1();

   }

}
What are Class loaders? Explain the types of class loader
What are Class loaders? - Class loaders are the part of the Java Runtime Environment that dynamically loads Java classes....
Dynamic loading
Dynamic loading - Classloader and its method LoadClass() is used for dynamic loading of a class...
Explain how to implement Shallow Cloning.
Shallow Cloning - Generally cloning means creating a new instance of a same class and copy all the fields to the new instance...
Post your comment