What is Variables in java
Variable(Container) is used to store values.
Types of Variables in java
Local variable:
Inside method
Scope inside method only.
Memory allocated when method starts and de-allocated when method ends.
Instance variable:
Inside class and outside method.
Scope within class.
Memory allocated when object gets created.
Static variable:
Variable with static keyword. Inside class and outside method.
Scope within class.
As soon as class gets loaded.
Local variables are typically used to store temporary values that are only needed within the scope of a specific method. They are not accessible outside of the method in which they are defined and are often used to store intermediate results or to pass values as arguments to other methods. They are useful for keeping the scope of a variable limited, which can make the code more readable and less prone to errors.
Instance variables, on the other hand, are used to store the state of an object. They are accessible from any method within the class and are often used to store values that are unique to each instance of the class. For example, an instance variable in a "Person" class might store the person's name, while another instance variable might store their age.
Static variables, also known as class variables, are used to store values that are shared by all instances of a class. They are also used to store data that is not specific to any particular object. For example, a static variable in a "Counter" class might store a global count that is incremented each time a new object is created. They are useful for maintaining state across all instances of a class. In addition, they are initialized only once when the class is loaded and are accessible even before creating any instance of the class.
In summary, variable types can be used to store values with different scope and lifetime, and it's important to choose the right type of variable based on the intended usage to make the code more readable, maintainable, and efficient.
Nesta página do site você pode assistir ao vídeo on-line Variable in Java: Understanding Its Types duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário ScriptQA 14 Janeiro 2023, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 180 vezes e gostou 9 espectadores. Boa visualização!