Java की Classes और Objects
In this page:
class ClassName {
dataType field;
void methodName() {
// body
}
}
ClassName object = new ClassName();
Defining a Class
एक class एक blueprint है जो describe करता है कि इसके objects में क्या data (fields) और behavior (methods) होंगे, लेकिन कोई memory असल में तब तक allocate नहीं होती जब तक आप इससे एक object न बनाएं — class खुद बस JVM के metadata में रहने वाला एक template है।
उदाहरण: Defining a Class
// Define the class Dog
class Dog {
String name;
int age;
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println("Class defined, no object yet");
}
}
Login to try C/C++/Java/PHP code in the editor
Instantiating Objects
new keyword एक object instantiate करता है, constructor चलाते हुए और heap पर उस specific instance के लिए memory allocate करते हुए। new की हर call एक distinct object produce करती है, भले ही आप हर बार identical constructor arguments pass करें।
उदाहरण: Instantiating Objects
class Dog {
String name;
}
public class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
Dog dog2 = new Dog();
System.out.println(dog1 == dog2); // false: distinct objects
}
}
Login to try C/C++/Java/PHP code in the editor
Working with Instance Methods
Instance methods एक specific object के data पर operate करते हैं और call होने के लिए एक object reference चाहते हैं (obj.method()), जो उन्हें बिना आपके object को explicitly एक parameter के रूप में pass किए सीधे उस particular object की fields पढ़ने और modify करने देता है।
उदाहरण: Working with Instance Methods
// Define the class Dog
class Dog {
// Declare name and set it to "Rex"
String name = "Rex";
void bark() {
// Print a line to the console
System.out.println(name + " says Woof!");
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Dog object and store it in dog
Dog dog = new Dog();
dog.bark();
}
}
Login to try C/C++/Java/PHP code in the editor
Object State Independence
किसी class से बना हर object class की instance fields की अपनी independent copy पाता है, इसलिए एक object की state बदलना उसी class के किसी दूसरे object पर zero effect डालता है — यह बिल्कुल वह core reason है कि objects अलग, independent entities model करने के लिए उपयोगी हैं।
उदाहरण: Object State Independence
// Define the class Dog
class Dog {
String name;
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Dog object and store it in dog1
Dog dog1 = new Dog();
// Create a new Dog object and store it in dog2
Dog dog2 = new Dog();
dog1.name = "Rex";
dog2.name = "Fido";
// Print a line to the console
System.out.println(dog1.name);
// Print a line to the console
System.out.println(dog2.name);
}
}
Login to try C/C++/Java/PHP code in the editor
Handling Null References
एक object variable जो assigned नहीं हुआ है (या explicitly null सेट किया गया है) बिल्कुल किसी object की ओर point नहीं करता; इस पर एक method call करना runtime पर एक NullPointerException throw करता है।
Dereference करने से पहले null जांचना Java code में सबसे आम defensive patterns में से एक है।
उदाहरण: Handling Null References
// Define the class Dog
class Dog {
void bark() {
// Print a line to the console
System.out.println("Woof");
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Dog dog = null;
// Check whether dog != null
if (dog != null) {
dog.bark();
} else {
// Print a line to the console
System.out.println("dog is null");
}
}
}
Login to try C/C++/Java/PHP code in the editor
- एक object बनाते समय
newभूल जाना, इसलिए variablenullहै। - एक
nullreference पर एक method call करना, जोNullPointerExceptionthrow करता है। - यह मान लेना कि वही data वाले दो objects
==हैं, जो references compare करता है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: