Variables in java:
In this chapter, we learn about variable types in Java. Variable is a name of a memory location. There are three types of variables: local variables (also known as local variables), properties (variables of instance-objects), and static variables.You must declare all variables before they can be used. The basic form of a variable declaration is as follows:Here, variable_type is a Java data type and variable is the name of the variable. To declare more than one variables with a particular type, you can use a comma-separated list of variables.
Here is an example of a valid variable declaration and initialization in Java:
Local variable in Java
Variables are declared in methods, constructors, or blocks.Variables are created when the method, constructor or block is entered and the variable is destroyed when the method, constructor or block terminates.
Access modifiers can not be used for local variables.
Local variables are only visible in methods, constructors, or blocks.
Local variables are internally executed.There are no default values for local variables, so local variables should be declared and an initial value should be assigned before use.
For example:
Here, age is the local variable. It is defined within the method age() and its scope is limited only to this method.
Attribute (or instance variable) in Java
- Attributes are declared in a class, but outside a method, constructor, or any other block.
- When a memory space is allocated to an object in a given heap, a slot for each attribute value is created.
- Properties are created when an object is created using the new keyword and is destroyed when the object is destroyed.
- Properties hold values that must be referenced by more than one method, constructor, or block, or key parts of the object state that must be present throughout the class.
- Attributes can be declared in class before or after use.
- Access modifiers can be provided for attributes.
- Attributes are visible to all methods, constructors, and blocks in the class. However, visibility for subclasses can be provided for these variables with the use of access specifiers.
- Properties have default values. With numbers, the default value is 0, with Boolean being false and with the object null. Values can be assigned in the declaration or in the constructor.
- Attributes can be accessed directly by calling variable names inside that class. However, with static methods and different classes (when the attribute is provided access), it should be called by using the fully qualified name as follows: ObjectReference.VariableName.
Class / static variables in Java:
Class variables are also known as static variables that are declared with the static keyword in a class, but outside a method, constructor, or block.There will be only one copy of each class variable for each class, regardless of how many objects are created from it.
Static variables are rarely used, in addition to being declared as constants. Constants are variables, which are declared as static variables, final variables, general / private variables. Constant variables never change from their initial values.
Static variables are stored in static memory.
Static variables are created when the program starts and is canceled when the program terminates.
Visibility is similar to attributes. However, most static variables are declared together as they must be available for use by the class.
The default value is the same as the attributes. With numbers, the default value is 0; with Boolean is false, and with the object is null. Values can be assigned in the declaration or in the constructor. In addition, values can be assigned in special static initialization blocks.
Static variables can be accessed by calling the class name: ClassName.VariableName.
When declaring class variables like public static final, variable names are in uppercase. If static variables are not public and final, the naming syntax is the same as local variables and attributes.
Note: If the variables are accessed from the outer class, the constant should be accessed as in the form: Student.FACULTY
Read more learn java point tutorial:
Comments
Post a Comment