Java Interview Questions - Variables and Block
1. What is variable?
A variable is a container that holds values that are used in a Java program. Every variable must be declared to use a data type.
2. How many numbers of variables present in java and its name?
Java supports 6 types of variable 1) static 2) instance 3) local 4) final 5) transient 6) volatile.
3. What is local variable?
A local variable in Java is a variable that’s declared within the body of a method. Then you can use the variable only within that method. Other methods in the class aren’t even aware that the variable exists.
4. Where local variable access?
Local variables are access only within the declared method, constructor or block.
5. What is static variable?
Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
6. How static variable can access?
Static variables can be accessed by calling with the class name. ClassName.VariableName.
7. What is instance variable in java?
In object-oriented programming with classes, an instance variable is a variable defined in a class (i.e. a member variable), for which each instantiated object of the class has a separate copy, or instance.
8. How to declare a constant in java and what is restriction for constant?
If the variable declared with final modifier then it is treated as constant in java. The restriction with final variable is at the time of declaration it should be initialized and can’t be reinitialized.
9. What is the procedure to call non - static variable?
Only by object
10. How to call local variable in java?
Directly within the same block and can’t access outside of the block.
11. What are the different blocks present in java?
There are two types of blocks present in java:
- Staticblock.
- Non-Staticblock.
12. What is static block?
If the block declared with static modifier is known as Static block. It is used to initialize the static data member; it is executed before main method at the time of class loading.
13. What is non - static block?
If the block declared without any static modifier known as non-static block.
14. If a variable is declared as private, where may the variable be accessed?
If a variable declared as private, it can only be accessed inside the same class.
15. Can you access non static variable in static context?
Now before finding answer of compiler error “non-static variable cannot be referenced from a static context”, let’s have a quick revision of static. Static variable in Java belongs to Class and its value remains same for all instance. static variable initialized when class is loaded into JVM on the other hand instance variable has different value for each instances and they get created when instance of an object is created either by using new() operator or using reflection like Class. newInstance (). So if you try to access a non static variable without any instance compiler will complain because those variables are not yet created and they don’t have any existence until an instance is created and they are associated with any instance. So in my opinion only reason which makes sense to disallow a non static or instance variable inside static context is not exist of instance.
16. What is the purpose of declaring a variable as final?
In java if user wants to declare any constant it should declare as final.
17. In case of declaring a variable which special character should allowed?
Underscore and Dollar.
18. How many times static block can execute?
Maximum for a single time static block can execute.
19. When static block execute?
Static block execute before main method execute by JVM.
20. When non-static block execute?
Non- Static block always execute before execution of constructor.
21. Execution of non-static variable depends upon whom?
As construct calling depend upon the user so execution of non-static block totally depend upon the user.
22. State the use of Final keyword?
Finally is the block of code that executes always. The code in finally block will execute even if an exception is occurred. finally will not execute when the user calls System. exit ().
23. How to access a non–static variable inside static block or method?
You can still access any non static variable inside any static method or block by creating an instance of class in Java and using that instance to reference instance variable. This is the only legitimate way to access non static variable on static context.
Or
A non static variable only can be access by a object, from a static method or static block and directly from a non static block only directly.