← Back to Core Java Course | Chapter 7: OOP Core | Lesson 2 of 11

Java की Classes और Objects

एक class एक blueprint है, एक cookie cutter जैसा, और एक object इससे बना एक cookie है। हर object का अपना details है लेकिन वही design follow करता है।
Syntax
java
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

java
// 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");
	}
}

Instantiating Objects

new keyword एक object instantiate करता है, constructor चलाते हुए और heap पर उस specific instance के लिए memory allocate करते हुए। new की हर call एक distinct object produce करती है, भले ही आप हर बार identical constructor arguments pass करें।

उदाहरण: Instantiating Objects

java
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
	}
}

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

java
// 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();
	}
}

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

java
// 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);
	}
}

Handling Null References

एक object variable जो assigned नहीं हुआ है (या explicitly null सेट किया गया है) बिल्कुल किसी object की ओर point नहीं करता; इस पर एक method call करना runtime पर एक NullPointerException throw करता है।

Dereference करने से पहले null जांचना Java code में सबसे आम defensive patterns में से एक है।

उदाहरण: Handling Null References

java
// 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");
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक object बनाते समय new भूल जाना, इसलिए variable null है।
  2. एक null reference पर एक method call करना, जो NullPointerException throw करता है।
  3. यह मान लेना कि वही data वाले दो objects == हैं, जो references compare करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.