DeclarationOfVariables
1)This is mainly used to store data which belongs to classes and objects.
Types:
1)Instance Variables (Unique for each object)
2)static Variables (Shared by all objects)
3)Local Variables (Exist only inside a method)
Instance
1)Declared inside a class but outside any method.
2)Instance variables are non-static variables declared within a class but outside of any method.
3)It has some unique properties
Example:(Unique id card)
class Student {
String name;
int age;
}
Student 1 -name = "Ajay",age = 21
Student 2 -name = "Ajay", age = 22{Both have their own values)
Static
1)Static variables are created at the class level only, not inside methods.
2)It memory is allocated only one time.
3)If one object changes its value, the change reflects in all other objects.
4)We can access static variables directly using the class name.
Local
1)Declared inside a method, constructor, or block
2)They are temporary and can be used only inside that method.
3)only accessible within the block.
Top comments (0)