Difference between field variable and local variable

What is the difference between a field variable and a local variable?

Local Variables:

- Local variable’s scope is within the block in which they were defined.
- They are alive as long as the block is executed.
- They can not have static access modifier

Field Variables:

- The life span is more than the local variables.
- The are alive as long as the instance of that class is active.
- They can have only ‘static’ access modifier.

What is the difference between a field variable and a local variable?

Field variables: Variable that are declared as a member of a class. OR Variables declared outside any method/constructor but inside the class block.
Scope: they can live as long as the instance they belong to is active.

Local variables: Variables that are declared within a method or a specific block of statements.
Scope: Live throughout the execution of the block
When do we need to flush an output stream?
If any bytes that are previously written and have been buffered by the implementation of the output stream...
Explain the purpose of garbage collection that the JVM uses
Garbage collection in Java identifies and discards the objects that are no longer needed by a program....
Explain how to read a line of input at a time
Input from the console is read from the System.in object and is then wrapped by an InputStreamReader which reads one character at a time......
Post your comment