Local variables vs Instance variables vs static variables

Local variables instance variables static variables or class variables
stored in stack stored in heap stored in data segment
declared with in a function or block declared at class level, not static declared at class level with static keyword
life time is with in that block or function life time is as long as object is alive life time is through out the program execution
will be created once functions starts executing will be created when the object is created will be created when the class is loaded
can be accessed with in that function can be accessible with object name from out side if it is not private. can be accessed with class name from outside, if it is not private
it is a property of function it is a property of an object. it is the property of a class.
each object will have its own set of instance variables. all objects shares same one copy of static variable.

Memory overview of a java program:

Before executing, all the programs will be in hard disk. Once it gets executed, the program will be loaded into memory. Once a program is loaded into memory, it is called as a process. A process is divided into many segments in RAM (Memory).
There are 4 important segments in memory: 1. Data Segment ->which stores static variables 2. Code Segment -> which stores your program code 3. Stack -> which stores local variables 4. Heap -> which stores instance variables, and objects.
Memory overview of a java program

Properties of variables:

* Local variables declared in a function cannot be accessed in other functions. * If you want to access the same variables in more than one function then declare it as instance variable. * Local variable will be created only after calling the function. * Instance variables will be created after creating the object. * If you want to access a variable with all the objects of the same class then declare it as "static variable". * Static variables are also called as class variable because all the objects of that class can share it or access it.
Program:

Write a program to find number of employees in a company.


emp object
File name: MainClass.java
    class  Emp
      {
	static int cnt;
	int eno,esal;
	String ename;
	public void init (int n,String m, int s)
	{
		eno=n;  
		cnt++;
		ename=m;
		esal=s;
	}
	public void disp()
	{
		System.out.println(eno);
		System.out.println(ename);
		System.out.println(esal);
	}
	public void getNoEmp()
	{
		System.out.println(cnt);
	}
              }
                          public class MainClass
                      {
	        public static void main(String[] args)
	    {
		Emp e1=new Emp();
		e1.init(1,"steve",2000);
		Emp e2=new Emp();
		e2.init(2,"Bill",3000);
		e1.getNoEmp();
		e2.disp();
	  }
     }
emp
Program:Write a program to create employees in a company. Every time we recruit a new employee we will pass only employee name and salary. We will not pass employee number. You have to write a generic logic to increment employee number automatically for each employee.
Emp1object
File name : MainClass.java
    class Emp
     {
	static int cnt;
	int esal,eno;
	String ename;
	public void init(String name,int sal)
	{
		ename=name;
		esal=sal;
		eno=++cnt;
	}
	public void disp()
	{
		System.out.println(eno);
		System.out.println(ename);
		System.out.println(esal);
	}
         }
     public class MainClass
      {
	public static void main(String[] args)
	 {
		Emp e1=new Emp();
		e1.init("steve",1000);
		Emp e2= new Emp();
		e2.init("Bill",2000);
		e1.disp();
		e2.disp();
	}
     }
Memory for above program:
emp12
Program:

Find a design flaw in below java program.

    class Pen
      {
	int nInk;
	boolean bcap_open;
	public void init()
	{
		nInk=100;
		bcap_open=false;
	}
	public void openCap()
	{
		bcap_open=true;
	}
	public void write()
	{
		nInk--;
	}
               }
                public class MainClass{
	public static void main(String[] args)
	{
		Pen p=new Pen();
		p.init();
		p.openCap();
		p.write();
	}
      }
The problem in above program is after creating the object , if programmer forgets to initialize the object then it ends up creating all un-initialized objects. That may lead to some dangerous scenarios. Since programmer is a human being, these type of errors are unavoidable. Solution for this problem is using constructors.