UNDERSTANDING STATIC


The data or variables , defined within a class are called instance variables . The code is contained within the methods . Collectively , the methods and variables defined within a class are called members  of the class .

There will be times when you will want to declare a class member that will be used independently of any object of that class . Till now you studied that , the class members must be accessed with an object of its class . However , it is possible to create a  member that can be used by itself , without reference to a specific instance . To create such a member , precede its declaration with a keyword static . When a member is declared static ,  it can be accessed before any objects of ts class are created and without reference to any object . You can declare  both methods and variables with the keyword static . 

The most common example about the static member is main() . main() is declared as static because it must be called before any objects exits .

Instance variables declared as static are essentially  global variables . At the time  of creation of object with respect to a class , all the instance variables except static variables will have a copy in the object . So whenever an object of its class are declared , no copy of a  static variable is made . static is a modifier .

STATIC FIELDS :

  1. The fields which are declared within a class with static modifier are called static fields .
  2. The memory of the instance fields without the static modifier will be stored in the heap organisation of object. But the memory of the static fields will be stored inside the constant pole .
  3. The default value of the static field is zero .
  4. In the memory of static field ( that is , in constant pole ) only one copy of memory is created and it can shared by multiple number of objects with the sane type .
  5. The static fields can be referred by using ClassName or object .

program on static fields :

StaicFields.java

PROGRAM 2 :

StaticFields2.java

In the above program , first through object ‘m’  you have given the values to instance variable x =100 , y=200 and static field z=300. Now , the instance variables with values will be stored in object ‘m’  but the static field z will be stored uniquely at constant pole memory with only one unit of space . Because the static memory will not store more than one value . It stores the last updated value .

Here , in the above program , first ,we have assigned ‘300’ to the static field ‘z’ and then we again assigned another value to the static field ‘z’ as ’30’ , so whatever the values you have stored previously ,  will be erased and new value will be updated .

Therefore , in the above program , the first time we entered 300 to the static field ‘z’ but it displaying the latest updated value of ‘z’ that is ’30’ .

If you still want the  memory pictorial representation of the  above program , just ping my blog . Thank you 🙂

STATIC METHODS : 

  1. The method which is defined within a class with static modifier , then that method will be treated as static method .
  2.  The static method will not perform any type of operations on the instance variables , it performs on the static fields ( class variables ).
  3. Whereas the instance variables and the local variables and instance methods will perform operations both on the non-static and the static data  .
  4. The static method can also be referred by using class name .
  5. unlike blocks the static methods are not the initialisers

PROGRAM ON STATIC METHOD :

StaticMethod.java

STATIC BLOCKS : 

                                                                                                               SYNTAX                                                                                      


STATIC BLOCK

  1. The static blocks are initialiser blocks , which are used for resource allocations , network connections , database connections .
  2. The static blocks are initial blocks , which are executed first whenever the class is loaded into the memory.
  3. After executing all the static blocks , then the JVM will search for the signature   ” public static void main(String args[]) ” .

PROGRAM ON STATIC BLOCK :

StaticAdd.java

 

CAN WE EXECUTE A JAVA PROGRAM WITHOUT main()

—> yes we can execute a java program without the main() : by using static blocks 

STATIC BLOCK  PROGRAM  WITHOUT  main() :

WITH EXCEPTION :

WITH EXCEPTION

 

WITHOUT EXCEPTION :  STATIC BLOCK WITHOUT MAIN()

 

Sorry for the inconvenience  . The program will be solved in shortly . 😦

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: