Step 1: Define a Class
A class is a blueprint for creating objects.
// Define a class named Car
class Car {
String brand;
int speed;
// Constructor
Car(String brand, int speed) {
this.brand = brand;
this.speed = speed;
}
// Method to display car details
void displayInfo() {
System.out.println("Brand: " + brand + ", Speed: " + speed + " km/h");
}
}
Step 2: Create an Object
Now, let's create an object from this class in the main method.
public class Main {
public static void main(String[] args) {
// Creating an object of Car
Car myCar = new Car("Toyota", 120);
// Calling a method on the object
myCar.displayInfo();
}
}
Explanation:
Class Definition: The Car class has properties (brand, speed) and a method (displayInfo).
Object Creation: new Car("Toyota", 120) creates an object and assigns it to myCar.
Method Call: myCar.displayInfo(); prints the car details.
1. Local Variable
A local variable is declared inside a method, constructor, or block and is only accessible within that scope. It cannot be accessed outside the method or block it is declared in.
2. Global Variable (Instance and Static Variables)
Java does not have global variables like in some other languages. However, instance variables (non-static fields) and static variables (class-level fields) serve a similar purpose.
Instance Variable (Non-Static Global Variable)
An instance variable is declared inside a class but outside a method. It belongs to an object, and each object has its own copy.
Rules of Creating Class:
- Syntax of a Java Class
class ClassName {
// Fields (Variables)
DataType variableName;
// Constructor
ClassName() {
// Initialization code
}
// Methods (Functions)
void methodName() {
// Method code
}
}
- Rules for Creating a Class 1️⃣ Class Name Rules
✅ Valid:
Must start with a letter, $, or _
Can contain letters, digits (0-9), _, and $
Cannot be a Java keyword (like class, public, etc.)
Java convention: Class names should start with an uppercase letter.
❌ Invalid:
class 123Car {} // ❌ ERROR: Cannot start with a number
class class {} // ❌ ERROR: "class" is a keyword
class my-car {} // ❌ ERROR: Cannot contain hyphens (-)
✅ Correct Example:
class Car {} // ✅ Correct (Capitalized)
class myCar {} // ✅ Correct (Camel case)
class $Vehicle {} // ✅ Valid (Uses $)
class _Bike {} // ✅ Valid (Uses _)
2️⃣ Class Declaration Rules
✅ Correct:
Use the class keyword to define a class.
The filename should match the class name if it is public.
A Java file can have multiple classes, but only one public class.
❌ Invalid:
public class Example {}
class Another {}
// ❌ ERROR: The filename must be Example.java
since it has a public class.
3️⃣ Access Modifiers for Classes
public class Animal {} // ✅ Can be accessed from anywhere
final class Car {} // ✅ Cannot be inherited
abstract class Shape {} // ✅ Must be extended, cannot create objects
4️⃣ Class Members (Fields and Methods)
Inside a class, you can define variables (fields) and methods (functions).
Fields and methods can have different access modifiers (public, private, protected).
✅ Example:
class Person {
String name; // Instance variable
int age;
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
5️⃣ Constructors Rules
A constructor must have the same name as the class.
It cannot have a return type.
If no constructor is defined, Java provides a default constructor.
✅ Example:
class Car {
String brand;
// Constructor
Car(String brandName) {
brand = brandName;
}
}
6️⃣ Inheritance Rules
A class can inherit another class using extends.
A class cannot inherit multiple classes (Java does not support multiple inheritance).
✅ Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
❌ Invalid Multiple Inheritance:
class A {}
class B {}
// ❌ ERROR: Java does not support multiple inheritance
class C extends A, B {}
7️⃣ Static and Non-Static Members
static members belong to the class, not instances.
non-static members belong to objects.
✅ Example:
class Example {
static int count = 0; // Shared among all objects
int instanceVar; // Each object has its own copy
}
Example: Fully Functional Class
// File: Person.java
public class Person {
String name;
int age;
// Constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}
// Method
void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
// Main method
public static void main(String[] args) {
Person p1 = new Person("Alice", 25);
p1.displayInfo();
}
}
✅ Output:
Name: Alice, Age: 25
Top comments (0)