Showing posts with label java object creation. Show all posts
Showing posts with label java object creation. Show all posts

Wednesday, July 23, 2025

Java Class Object - Explained with Example क्लास ऑब्जेक्ट - उदाहरण सहित

An object is a real-world entity that contains both properties (data) and behaviors (methods). In Java, an object is an instance of a class that represents its existence. For example – pen, pencil, calculator, car, or mobile. A class object can be logical or physical and it holds the following:

वास्तविक दुनिया की एक ऐसी इकाई जिसमें गुण (डेटा) और व्यवहार (क्रियाएँ) दोनों होते हैं, उसे ऑब्जेक्ट कहा जाता है। जावा में, एक ऑब्जेक्ट किसी क्लास का instance (उदाहरण) होता है, जो उस क्लास के अस्तित्व को दर्शाता है। जैसे – पेन, पेंसिल, कैलकुलेटर, कार, मोबाइल आदि। एक क्लास ऑब्जेक्ट तार्किक या वास्तविक हो सकता है जिसमें निम्न होते हैं:

Property – Represents the class's variables.

गुण (Property) – क्लास के वेरिएबल को दर्शाता है।


Behavior – Represents the class's methods.

व्यवहार (Behavior) – क्लास की मेथड को दर्शाता है।

In real life, a student is an object with properties like roll number, name, and marks, and behaviors like mark sheet generation, admit card printing, or scholarship status.

वास्तविक जीवन में एक स्टूडेंट ऑब्जेक्ट होता है, जिसके पास रोल नंबर, नाम और प्राप्तांक जैसे गुण होते हैं, और मार्कशीट बनाना, एडमिट कार्ड निकालना या स्कॉलरशिप लेना जैसे व्यवहार होते हैं।


🧰 How to Create Class Object in Java जावा में क्लास ऑब्जेक्ट कैसे बनाएं

In Java, an object of a class is created using the new keyword. 

जावा में किसी क्लास का ऑब्जेक्ट new कीवर्ड के माध्यम से बनाया जाता है।

Syntax

ClassName variable_name = new ClassName();

Example

public class Student {
  int rollno;
  String sname;
  float marks;

  void sinfo() {
    System.out.println("Roll Number: " + rollno);
    System.out.println("Name: " + sname);
    System.out.println("Marks: " + marks);
  }

  public static void main(String[] args) {
    Student s = new Student();
    s.rollno = 1;
    s.sname = "Ajay";
    s.marks = 421;
    s.sinfo();
  }
}

In the above example, the main() method creates an object s of the Student class and accesses its variables using that object.

उपरोक्त उदाहरण में main() मेथड का उपयोग Student क्लास का s नामक ऑब्जेक्ट तैयार करने के लिए किया गया है, और उस ऑब्जेक्ट के माध्यम से क्लास के वेरिएबल्स को एक्सेस किया गया है।


🛠️ Other Ways to Create Object in Java

In Java, objects can also be created using the following ways:

  • Using new keyword

  • Using newInstance() method

  • Using clone() method

  • Using Deserialization

  • Using factory() method


🛠️ जावा में ऑब्जेक्ट बनाने के अन्य तरीके

जावा में ऑब्जेक्ट निम्नलिखित तरीकों से भी बनाए जा सकते हैं:

  • new कीवर्ड का उपयोग करके

  • newInstance() मेथड के द्वारा

  • clone() मेथड के द्वारा

  • Deserialization के द्वारा

  • factory() मेथड के द्वारा

Expression Statement in Java जावा में एक्सप्रेशन स्टेटमेंट

In Java, expression statements are valid expressions that end with a semicolon ;. These are used to perform operations like assigning values, calling methods, or creating objects. Java supports mainly four types of expression statements: assignment expressions, increment/decrement expressions, method calling, and object creation expressions.

जावा में, एक्सप्रेशन स्टेटमेंट ऐसे वैध एक्सप्रेशन होते हैं जो अर्धविराम ; के साथ समाप्त होते हैं। इनका उपयोग वैल्यू असाइन करने, मेथड कॉल करने या ऑब्जेक्ट बनाने जैसे कार्यों को करने के लिए किया जाता है। जावा मुख्यतः चार प्रकार के एक्सप्रेशन स्टेटमेंट को सपोर्ट करता है: असाइनमेंट एक्सप्रेशन, इन्क्रीमेंट/डीक्रीमेंट एक्सप्रेशन, मेथड कॉलिंग और ऑब्जेक्ट क्रिएशन एक्सप्रेशन।

1️⃣ Assignment Expression

Assignment expressions are used to assign values to variables. These include simple assignments as well as compound assignments like +=, -=, *=, etc.

a += 5;
b *= 7;
c = 3;

असाइनमेंट एक्सप्रेशन का उपयोग वैरिएबल्स को मान असाइन करने के लिए किया जाता है। इसमें साधारण असाइनमेंट के साथ-साथ +=, -=, *= जैसे संयुक्त असाइनमेंट भी शामिल होते हैं।


2️⃣ Increment and Decrement Expression

These expressions are used to increase or decrease the value of a variable using prefix or postfix operators.

++a;
--b;
c++;
d--;

इन एक्सप्रेशन्स का उपयोग वैरिएबल के मान को बढ़ाने या घटाने के लिए किया जाता है। इसमें प्रीफिक्स (++a, --b) और पोस्टफिक्स (c++, d--) दोनों प्रकार के ऑपरेटर आते हैं।


3️⃣ Method Calling Expression

A method call can also be an expression statement in Java, regardless of whether the method returns a value or not.

obj.factorial();

जावा में, कोई भी मेथड कॉल एक एक्सप्रेशन स्टेटमेंट हो सकता है, चाहे वह मेथड कोई मान लौटाए या नहीं।


4️⃣ Object Creation Expression

Creating an object using the new keyword is also a valid expression statement in Java.

Test t = new Test();

new कीवर्ड का उपयोग करके ऑब्जेक्ट बनाना भी जावा में एक वैध एक्सप्रेशन स्टेटमेंट होता है।


➕ Arithmetic Expression and Its Solution

Arithmetic expressions involve the use of mathematical operators like addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). A single-mode arithmetic expression uses operands of the same data type, such as integer or floating-point. Let’s assume A = 10 and B = 5, and see how different arithmetic operators work.

अंकगणितीय एक्सप्रेशन में गणितीय ऑपरेटरों जैसे जोड़ (+), घटाव (-), गुणा (*), भाग (/) और शेषफल (%) का उपयोग होता है। एक सिंगल-मोड अंकगणितीय एक्सप्रेशन में सभी ऑपरेण्ड समान प्रकार के होते हैं, जैसे कि इन्टिजर या फ्लोट। मान लीजिए A = 10 और B = 5, तो देखें विभिन्न ऑपरेटर कैसे कार्य करते हैं।


🔢 Table of Arithmetic Expressions

Operator Operation Expression & Result
+            Addition              A + B = 15
- Subtraction A - B = 5
* Multiplication A * B = 50
/ Division A / B = 2
% Modulus A % B = 0


✅ Conclusion

Understanding expression statements is key to writing functional Java programs. They allow you to perform tasks like assigning values, updating variables, calling functions, or instantiating objects. Arithmetic expressions help in performing mathematical operations efficiently.

एक्सप्रेशन स्टेटमेंट्स को समझना कार्यात्मक जावा प्रोग्राम लिखने के लिए आवश्यक है। ये वैल्यू असाइन करने, वैरिएबल अपडेट करने, फंक्शन कॉल करने या ऑब्जेक्ट बनाने जैसे कार्यों को करने में मदद करते हैं। अंकगणितीय एक्सप्रेशन गणनात्मक कार्यों को कुशलता से करने में सहायता करते हैं।

📚 Other Stream Classes in Java जावा में अन्य स्ट्रीम क्लासेस

In Java, apart from basic byte and character streams, there are several specialized stream classes that provide advanced input-output operat...