- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Class and Object
In Java, every program revolves around Object-Oriented Programming (OOP) principles. At its core, a class is a blueprint or a template, and an object is a concrete instance of that blueprint. Think of a class as the architectural drawing of a house; the drawing itself isn't a house you can live in, but it defines exactly how many rooms the house will have. An object is the actual house built using those plans.
UserAccount instead of useraccount). This makes your code follow standard Java naming conventions and improves readability for other developers.
Class Definition:
- Define a class using the class keyword followed by the class name. Inside the curly braces, you define the fields (data) and methods (behavior) that make up the class.
- Example:
class Car {
// Fields (Attributes)
String brand;
int year;
boolean isRunning;
// Method (Behavior)
void startEngine() {
isRunning = true;
System.out.println("The " + brand + " engine is now running.");
}
void drive() {
if (isRunning) {
System.out.println("Driving the " + brand);
} else {
System.out.println("Start the engine first!");
}
}
}
Object Creation:
- To use a class, you must create an instance of it. This is done using the new keyword, which tells Java to allocate memory for a new object on the "Heap."
- Example:
Car myCar = new Car();
Car myCar; does not create an object. It only creates a reference. You must use the new keyword to actually instantiate it in memory.
Accessing Attributes and Methods:
- Once an object is created, you interact with it using the dot (.) operator. This allows you to assign values to fields or trigger the logic defined in your methods.
- Example:
myCar.brand = "Toyota";
myCar.year = 2020;
// Calling a method to perform an action
myCar.startEngine();
myCar.drive();
NullPointerException. Always ensure your object is instantiated before calling its methods.
Multiple Objects:
- One of the most powerful features of classes is the ability to create multiple independent objects. Each object maintains its own "state"—meaning the values of its fields are separate from other objects of the same type.
- Example:
// Creating a second, independent object
Car friendCar = new Car();
friendCar.brand = "Honda";
friendCar.year = 2018;
// Each object operates independently
myCar.drive(); // Uses "Toyota" data
friendCar.drive(); // Uses "Honda" data
In a real-world application, like an e-commerce site, you might have a Product class. While you only write the code for the Product class once, you would create thousands of objects from it—one for each item in your inventory (e.g., "Laptop", "Smartphone", "Headphones").
Summary
Java's class and object concept allow for the creation of modular and reusable code, promoting code organization and maintainability. By treating your code as a collection of interacting objects rather than just a list of instructions, you can build complex systems that are easier to debug and scale. Master these fundamentals, as they are the building blocks for more advanced topics like Inheritance and Polymorphism.