QA's approach 2 Java - Variable in Java

Variables

Variable is the basic unit in a java program.It is a container to hold only one value.
where in the data is stored in Temporary memory, hence it hold the values within the life cycle of that variable or onto program termination.



The Java programming language defines the following kinds of variables:

Instance Variables (Non-Static Fields) -

1. Objects/instances of class, store their individual states in "non-static fields", that is, fields declared without the static keyword. 
2. Need to be initialized explicitly 
3. Provided memory on object creation in oldgen/newGen area's in Heap memory.  
4. Lifetime of the variable is upto that particular object or instance of class only.
5. Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

Note: null may be assigned to any variable, except variables of primitive types.


Class Variables (Static Fields) 

A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.

1. Static methods (in fact all methods) as well as static variables are stored in the PermGen section or Method Area, since they are part of the reflection data (class related data, not instance related).
2. Static variables were initialized as they were declared. Sometimes, if you need to do additional initialization work. You can put it into a static initialization block. 
3. Static data (fields and members) are loaded into memory on class loading and remain until the class is unloaded from memory (typically on JVM termination).
4. Static variables can be looked as Global variables, which is accessed & shared commonly across.
5. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in ClassName.VariableName
\
Example analogy:

 A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change. 


Local Variables:

1. Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. 
2. There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared, which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.


Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

Selenium 4 absolute beginners - How to create Batch execution file

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?