Java / Guided lesson

Understand Java Classes and Objects

Build a strong foundation in Java programming. Explore classes, objects, methods, constructors, inheritance, and object-oriented programming with practical coding examples.

Browse all Java topics

Worked example

Java is a class-based, object-oriented programming language. A class defines the structure and behavior of objects. Objects are instances of classes. Constructors initialize objects, while methods define their behavior.

class Student {
    String name;
    int marks;

    Student(String name, int marks) {
        this.name = name;
        this.marks = marks;
    }

    void displayDetails() {
        System.out.println("Name: " + name);
        System.out.println("Marks: " + marks);
    }
}

public class Main {
    public static void main(String[] args) {
        Student student = new Student("Rahul", 90);

        student.displayDetails();
    }
}

Expected output

Name: Rahul
Marks: 90

Check your understanding

What is the difference between a class and an object in Java?

Show answer

A class is a blueprint that defines fields and methods. An object is an instance of that class created in memory. Multiple objects can be created from the same class, each with its own instance data.

Try it yourself

Add a method called checkResult() that prints Pass when marks are greater than or equal to 40, otherwise prints Fail.

Show solution
void checkResult() {
    if (marks >= 40) {
        System.out.println("Pass");
    } else {
        System.out.println("Fail");
    }
}

// Add this method inside the Student class.
// Call student.checkResult() inside main.

// Output: Pass

Java / Full curriculum

All Java Topics

Choose a topic to study the explanation, code, output, questions, and practice exercises.

Lesson 1 / 57

Java · Lesson 1 of 57

Java Introduction and Hello World

1. Understand the idea

Java is a language for building apps, websites, and many other programs. You write instructions in a class, and the Java tools prepare them so a computer can run them.

Think of a real-life example

Think of writing a recipe, then giving it to a chef who knows how to follow Java recipes.

2. Read the example

Read the code slowly. Before checking the output, try to guess what will happen.

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, AskBun!");
    }
}

What happens in this code?

Main is the class name. The main method tells Java where to start. println shows Hello, AskBun! on the screen.

Expected output

Hello, AskBun!

3. A mistake to avoid

Save this example as Main.java when the public class is Main. Java pays attention to uppercase and lowercase letters.

New words explained simply
jvm
Java Virtual Machine: the software that runs compiled Java code.
bytecode
The instructions Java compiles into for the JVM to run.
class
A plan for making objects.
static
Belongs to the class rather than one object.

4. Check your understanding

What is the JVM?

The Java Virtual Machine executes Java bytecode.

5. Try it yourself

Try the task on your own first. It is okay to get it wrong—that is part of learning. Then open the solution to compare.

Print your own greeting.

Back to all subjects