Member type in java

Instance variable:

Any variable that you declare at class level which is not static.

Instance function:

All non-static functions of a class are called instance functions. Note: Constructors also belongs to instance function.

Static variable:

Any variable that you declare at class level which is static is called static variable. They are also called as "class variables".

Static function:

Any function in a class which is declared as static are called as static functions. They are also called as "class functions" or "class methods".

Rules to access the members:

1) You can access instance and static variable from a instance method or instance function. 2) You can access other instance method and static methods from a instance method. 3) You can access only static variables from a static method. 4) You can access other static methods from a given static method.
Example:

    class Member
    {
    int i=10;
    private int j=20;
    static int k=30;
    public Member()
    {
      System.out.println("constructor");
    }
   public void display()
   {
     System.out.pritnln(i);
     System.out.pritnln(k);
   }
   public void fun()
   {
    System.out.println(k);
  display();
  }
  public static void add()
  {
   System.out.println(i+j+k);//compile error, can't  
                             //access I,j
  }
  public static void All()
  {
    fun();//compile error, can't access fun
    add();
   }
 }

How to use & access static variables with class name

      class Members
        {
           int i=10;
           private int j=20;
           static int k=30;//can be accessed with class name
           public Members()
           {
            System.out.println("constructor");
            }
            public void disp()
           {
            System.out.println(i);
            System.out.println(k);
           }
           }
          public class MainClass
         {
            public static void main(String[ ] args)
           {
           System.out.println(Members.k);//accessing statics
           Members m=new Members();
           System.out.println(m.i);
           }
         }

Note: To access a class variable or method you don't need to create an object.
Program:

Write a server class, in such a way that you can't create more than 5 objects to that class


File name : Client.java
            class Server
            {
            public static int cnt=0;
            private int i;
             public Server()
             {
              i=10;
             }
             }
             class Client
             {
             public static void main(String[] args)
             {
             if(Server.cnt!=5)
             {
             Server s=new Server();
             }
            }
}

Static methods:

There are 2 scenarios where you can use static methods. 1. If you are writing any generic method common for all the objects then use static methods. 2. If static variables are private, and if some one wants to access those variables, then you can do it through public static methods.
Note: 1. Don't touch instance variables or instance methods from static methods 2. If a method is not accessing any instance variables, better to convert it as static method.
Program: Write a server class which contains a method which returns total number of objects created for that class so far.
File name: Client.java
       class Server
        {
	private static int cnt=0;
	private int i;
	public Server()
	{	
	  cnt++;
	  i=10;
	}
	 public void disp()
         {
	   System.out.println(i);
	  }
	 public static int getcnt()
	 {
	   return cnt;
	}
        }
        public class Client
        {
	 public static void main(String[] args)
	 {
	     int cnt=Server.getcnt();
	      if(cnt!=5)
	      {
	       Server s=new Server();
	      }
	    }
            }

Program:

Write a calculator class which contains 2 methods one adds two integers, and other adds 2 strings.


File name: MathsDemo.java
                public class MathsDemo    
                 {
	        public static void main(String[] args)
                {
		// static methods can be accessed directly with class name
		  Calculator.add(10, 20);
		  Calculator.add("hai", " how are you");
	         }
                }

                class Calculator
                {
	          /* since below two are utility methods which are not 
                     Accessing any instance variables, so we made it 
                      as static methods */
	       public static void add(int i, int j)
                {
                   System.out.println(i+j);
	        }
	        public static void add(String s, String t)   
                {
		System.out.println(s+t);
	        }
               }