GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Classes and Objects in Java - GeeksforGeeks Classes and Objects in JavaLast Updated : 7 Oct, 2026 In Java, classes and objects form the foundation of Object-Oriented Programming (OOP). They help model real-world entities and organize code in a structured way.
For Example: Dog is a class, Tommy is an object of that class. ![]() Java ClassA class is a user-defined data type that groups related data and behavior together. It can contain fields, methods, constructors, nested classes, and interfaces.
Output 10 Alice Explanation: This code defines a Student class as a blueprint with data members and a constructor to initialize them. An object s1 is created in main(), which allocates memory and accesses the class fields to display values. Java ObjectsAn object is an instance of a class created to access its data and operations. Each object holds its own state.
Objects mirror real-world items such as customer, product or circle. Non-primitive objects are stored on the heap while their references remain on the stack. ![]() Object InstantiationCreating an object is known as instantiation. All instances of a class share structure and behavior while storing different state values. ![]() Declaration:
This only declares a reference. The object is not created and the reference holds null. Initialization:
![]() The new operator allocates memory and invokes the constructor. Example: Defining and Using a Class
Output Name is: tuffy Breed age and color are: papillon 5 white Explanation: This creates a Dog object named tuffy using the Dog constructor inside main(), which initializes name, breed, age, and color. When System.out.println(tuffy) is executed, the overridden toString() method is called to display the object’s data.
Example: Initialize Object by using Method/Function
Output Visual Studio - 0.0 IntelliJ IDEA - 4999.0 Explanation: This code defines a Product class with instance variables name and price, which are initialized using a constructor. In the main() method, two objects (p1 and p2) are created with different values. Each object maintains its own state, and the getter methods are used to access and display the values of each object independently. Ways to Create Object in JavaJava supports four standard approaches. 1. Using new Keywordnew Keyword is most direct way to create an object.
2. Using ReflectionUsed for dynamic class loading as seen in frameworks like Spring.
Output Student@1dbd16a6 3. Using clone() methodclone() creates a copy of an existing object. The class must implement Cloneable.
Output GeeksForGeeks Explanation: Here, creates a new Geeks object g2 by cloning the existing object g1 using the overridden clone() method. The cloned object copies the field values of g1, which is confirmed by printing g2.name. 4. Using DeserializationDe-serialization is a technique of reading an object from the saved state in a file. Object is recreated from a stored byte stream.
Output Student: Alice Explanation: This first serializes a Student object to the file student.ser using ObjectOutputStream. It then deserializes the object using ObjectInputStream, recreating the Student object and printing it via the toString() method. Anonymous ObjectsAnonymous objects are objects created without assigning them to a reference variable. They are generally used when an object is needed only once.
Output Tommy is barking Explanation: new Dog("Tommy") creates a Dog object, and bark() calls its method to print the dog's name followed by "is barking". Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |