INSTANCE V/S STATIC

INSTANCE VARIABLES:
        Instance variables are declared inside the class but not inside the method
eg:
class Test {
int data = 15; //instance variable
floor pi = 3.14f;
}

PROPERTIES OF INSTANCE VARIABLES:
1 Instance variable always get default variables
2 Cannot be reinitialized directly within the class

class Test {
int data = 15; 
data = 20;//error
}

class Test {
int data = 15; 
void someMethod(){
data = 20; //
allowed }}


************************************


STATIC VARIABLES:
            Instance variables are declared within the class and we need to use static keyword
class Test {
static int data = 15; //only one copy is created per class 
data = 20;//error
}
        


Popular posts from this blog

36 LOGICAL "OR" OPERATOR

Arrays

ENCAPSULATION