5 Question Related To Variables and Block
5 Question Related To Variables and Block
1. What is variable?
Ans:
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.
Ans:
Java supports 6 types of variable 1) static 2) instance 3) local 4) final 5)
transient 6) volatile
Ans:
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.
Ans:
Local variables are access only within the declared method, constructor or block.
Ans:
Class variables also known as static variables are declared with the static
keyword in a class, but outside a method, constructor or a block.
Ans:
Static variables can be accessed by calling with the class name.
ClassName.VariableName.
Ans:
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.
Ans:
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.
Ans:
Directly within the same block and can’t access outside of the block.
Ans:
There are two types of blocks present in java
i. Static block.
ii. Non-Static block.
Ans:
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.
Ans:
If the block declared without any static modifier known as non-static block.
Ans:
If a variable declared as private, it can only be accessed inside the same class.
Ans:
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.
Ans:
Underscore and Dollar
Ans:
Maximum for a single time static block can execute.
Ans:
Static block execute before main method execute by JVM.
Ans:
Non- Static block always execute before execution of constructor.
Ans:
As construct calling depend upon the user so execution of non-static block totally
depend upon the user.
Ans:
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 ().
Ans:
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.