Java के लिए static, final और super ये keywords काफी प्रभावशाली है |
इनके बारे में देखे तो instance variable जब class में declare किया जाता है, तब उस class का object या instance बनाने के बाद उसकी copy; object पर होती है | अगर एक class के multiple objects हो तो हर object पर instance variable की अलग-अलग copies होती है |
For Static Keyword
Static keyword के दो हिस्सों में काम करता है |- Static/Class Variable
- Static/Class Method
इनके बारे में देखे तो instance variable जब class में declare किया जाता है, तब उस class का object या instance बनाने के बाद उसकी copy; object पर होती है | अगर एक class के multiple objects हो तो हर object पर instance variable की अलग-अलग copies होती है |
1. Static/Class Variable
- Static Variable ये अपने class के लिए होता है, इसीलिए इसे Class Variable कहते है |
- Static Variable Object या instance के लिए नहीं होता |
- Static Variable के लिए object; create करने की जरुरत नहीं होती |
- Static Variable को class के नाम से access किया जाता है |
- Class के हर object पर static variable की एक-एक copy होती है |
- अगर Memory के बारे में देखे तो Static variable काफी Memory save करता है |
- Static Variable; Instance Variable जैसा इस्तेमाल हो सकता है, लेकिन Instance Variable; Static Variable जैसा इस्तेमाल नहीं हो सकता |
int a; // Instance Variable static int b; // Static/Class Variable
Syntax for Accessing Static Variable
//Accessing for Instance Variable by Class Instance/Object class_instance.variable_name = variable_value; //Accessing for Static/Class Variable by class name class_name.static_variable_name = static_variable_value;
Example for Accessing Static Variable
//Accessing for Instance Variable by Class Instance/Object obj.a = 5; //Accessing for Static/Class Variable by class name Sample.b = 10;
Example for Static Variable
Source Code :
class Sample{
static int a;
Sample(){
a++;
}
public static void main(String args[]){
Sample s1 = new Sample();
Sample s2 = new Sample();
System.out.println("Value of a : " + a);
System.out.println("Value of a : " + a);
}
}
Output :
Value of a : 1 Value of a : 1
2. Static/Class Method
- Static Method ये अपने class के लिए होता है, इसीलिए इसे Class Method कहते है |
- Static Method; Object या instance के लिए नहीं होता |
- Static Method के लिए object; create करने की जरुरत नहीं होती |
- Static Method को class के नाम से access किया जाता है |
- Class के हर object पर Static Method की एक-एक copy होती है |
- Static Method; Instance Method जैसा इस्तेमाल हो सकता है, लेकिन Instance Method; Static Method जैसा इस्तेमाल नहीं हो सकता |
int disp(){ }; // Instance Method
static int show(){ }; // Static/Class Method
Syntax for Accessing Static Method
//Accessing for Instance Method by Class Instance/Object class_instance.instance_method_name(); //Accessing for Static/Class Method by class name class_name.static_method_name();
Example for Accessing Static Method
//Accessing for Instance Method by Class Instance/Object obj.disp(); //Accessing for Static/Class Method by class name Sample.show();
Example for Static/Class Method
Source Code :
class Sample{
void disp(){
System.out.println("Calling with object.");
}
static void show() {
System.out.println("Calling without object or class.");
}
public static void main(String args[]){
Sample s = new Sample();
s.disp(); //Calling with instance
show(); //Calling without instance
Sample.show(); //Calling with class
}
}
Output :
Calling with object. Calling without object or class. Calling without object or class.







No comments: