2.Difference between static and non-static variables.
Ans:-
Static Variables:-

  •  When a variable is declared as static, then a single copy of the variable is created and shared among all objects at a class level.
  • Static variables are, essentially, global variables. All instances of the class share the same static variable .
  • Static variables can be accessed using class name
  • Static variables can be accessed by static and non static methods.
  • Static variable is like a global variable and is available to all methods.
Non-static Variable:-
  • Non static variables can be accessed using instance of a class.
  • Non static variables cannot be accessed inside a static method.
  • Non static variables do not reduce the amount of memory used by a program.
  • Non static variables are specific to that instance of a class.
  • Non static variable is like a local variable and they can be accessed through only instance of a class.

Comments

Popular posts from this blog

java Assignment Questions 4