Wednesday, February 24, 2016

What is static and non-static concept in Java?

Before you read this post, read my post " meaning of static and dynamic in the context of programming".

In Java, we deal with objects, the concept of static and nonstatic is related to objects itself.

The other term used in Java for static is class variable, for nonstatic it is instance variable.

In a nutshell, nonstatic property describes instance property, i.e. object property,and it is accessed with object, where as static property describes class property(i.e common for all objects) and it is accessed with class name in which it is declared.

// Example
class Account 
{

 double balance; // instance property
 static double min_balance; // class property
 ----
 ----
}
In the above example, for every account the state(value) of balance changes, hence it is nonstatic(i.e changing) where as for every account the state of min_balance is same(not changing), i.e common for all object. So it is static.

// Accessing static and nonstatic data/method

class Bank
{
 public static void main(String arg[])
 {
  Account a1=new Account();
  a1.balance=5000; // Accessing nonstatic data
  System.out.println("Balance is"+a1.balance);
  System.out.println("Minimum Balance is"+Account.min_balance);
}

Can you tell me what's the point in doing so? please reply...


1 comment:

  1. sir this is the great stuff...i was possibly searching for...easy to understand..tnq sir

    ReplyDelete