what is difference between static and non-static variables
Answers were Sorted based on User's Feedback
Answer / sv
static variable retains its value like a global variable,
but visible with in the scope.
While non static variable will not be retained one it come
out of the scope or block. Again the visibility is local to
the block.
| Is This Answer Correct ? | 53 Yes | 8 No |
Answer / udit rawat
static variable take memory first at linking and loading time
while non static variable take memory at compile time
| Is This Answer Correct ? | 35 Yes | 11 No |
Answer / mahesh_b.tech@2008
Non-static variables:
Instance block can be executed automatically when you were
creating the object.
Instance variables can be called by using with the object.
Instance variables can not called by using the class name.
memory will be alocated for instanse variables when you
create the object.
Static variables:
static block will be executed automatically when jvm(Java
Virtual Machine)loading the class into memory.
static varisble can be called using both objectname&class
name.
memory will be allocated when jvm loads the class.
| Is This Answer Correct ? | 13 Yes | 4 No |
Answer / sengar
static variable will not be change value all time but non
static value can be changed according requirement .
without giving any value static variable automatically
initialize by zero but in non-static case not the same
case.
| Is This Answer Correct ? | 11 Yes | 2 No |
Answer / daxini
Static :
A static variable is defined for the class itself and exists independently of any object of that class.
non-static:
a non static variable is defined for object of the class thus it is bound to an instance of its class
| Is This Answer Correct ? | 10 Yes | 2 No |
Answer / narender vadhava
static variables have their fix values within a program but
in case of non-static variables their values are changable
within the same program.
syntax of static variable:-
static int s;
while
non static variables declared only by using general basic
data type.
as:-
int s;
| Is This Answer Correct ? | 14 Yes | 12 No |
Answer / arun kumar
Initialization of static variables is at compile time,
but in case of non-static variables is at run time.
| Is This Answer Correct ? | 6 Yes | 7 No |
Answer / basharat ali
satatic variable use any where but non-static variable can
use with in a block..............
| Is This Answer Correct ? | 2 Yes | 6 No |
Answer / achal ubbott
The above answers are correct. A local static variable is
stored in heap. But a local variable is stored onto stack.
A Global variable made static can't be accessed from a
function outside the file using extern. So it limits the
scope.
| Is This Answer Correct ? | 2 Yes | 10 No |
What is the size of a vector?
How long does it take to get good at leetcode?
what is software cycle? What is a mission critical system ? What is the important aspect of a real-time system ? Explain the difference between microkernel and macro kernel. Give an example of microkernel.Why paging is used ? Which is the best page replacement algo and Why ? What is software life cycle ? How much time is spent usually in each phases and why Which one do U want to work if selected in Honeywell ? Which are the different types of testing ? What is a distributed system ? Some questions about CSP. Which languages do U know ? What are the differences between Pascal and C. questions from Compiler construction and Lisp. Which are the different computer architecture? What is the requirement in MIMD ? What is the difference between RISC and CISC processors ? Difference between loosely coupled and tightly coupled systems ? What is an open system?
What is input operator in c++?
What are the comments in c++?
In a class only declaration of the function is there but defintion is not there then what is that function?
Can a program run without main in c++?
Write a Program for find and replace a character in a string.
What is the difference between global variables and static varables?
How does atoi function work?
what is object?
If dog is a friend of boy and boy is a friend of house, is dog a friend of house?