Prev Next

Java / Variable

Could not find what you were looking for? send us the question and we would be happy to answer your question.

1. Is it possible to access a non static variable in static context?

A static variable in Java is at class level and shared among all its instances. A static variable is initialized when the class is loaded by the JVM.

When we try to access a non-static (instance-level) variable from the static context, we encounter compilation error as instance variables are not created and it is not associated with any instance.

2. Is null, main, exit, delete are keywords in Java?


3. Explain integer number with leading zeros in Java.

The number starting with 0 represents an octal digit.

4. What is transient variable in Java?

Transient variable cannot be serialized.

For example if a variable is declared as transient in a class implementing Serializable interface and the class is persisted to an ObjectStream, the value of the variable is ignoerd and not written to the stream. When the class is retrieved from the ObjectStream the value of the variable becomes null.

5. What is shadowing in Java?

Shadowing refers to the practice of using two variables with the same name within scopes that overlap. When shadowed, the variable with the higher-level scope is hidden because the variable with lower-level scope overrides it. The higher-level variable is then shadowed.

You can access a shadowed class or instance variable by using its fully qualifyed name which is the name of the class that contains it. Shadowing is also known as variable shadowing.

public class Shadowing {

	int i = 5;

	public void setI(int i) {
		this.i = i;


In the above example, the variable 'i' is shadowed in the setter and this keyword is used to refer to the instance variable 'i'.

6. Different types of variables in Java.
  • Instance or non static variables,
  • Static or class variables,
  • Local or method variables,
  • and parameters.
7. Difference between static type and dynamic type programming languages.

A language is statically typed if the type of a variable is known at compile time. For example in Java, C and C++, programmer must specify what type each variable is.

A language is dynamically typed if the type is associated with run-time values. Example programming languages are perl, python and ruby.

8. What are local variables in Java?

A local variable is one that exists inside a method or a block of code and exists as long as that method or block is executing. Once the program reaches the end of the method (or block), the local variable disappears from memory. The next time the method is called, a completely new version of the local variable comes into existence. One of the most common types of local variables is a parameter.

9. Is local variable thread safe in java?

Yes. All local variables defined in your program will be allocated memory in the stack. So, When you create a thread it will have its own stack created. Two threads will have two stacks and one thread never shares its stack (that is, local variables) with other thread.

Class Variables (Static Variables)

Comments & Discussions