Getting Started with Java: Understanding Class, Object, Main Method, Memory, and More

When starting your journey with Java, it’s easy to get overwhelmed by keywords and structures like public static void main(String[] args). But once you break them down, they’re not as scary as they seem. In this blog, I’ll explain Java basics using a simple program and go deep into how it all works behind the scenes — including memory, threads, and more.


Java Basics: Class, Object, Method, and Function

Class

A class in Java is like a blueprint. It defines the structure and behavior (properties and methods) of objects.

class Dog {
    String name;

    void bark() {
        System.out.println("Woof!");
    }
}

Object

An object is an instance of a class. You create it using the new keyword.

Dog myDog = new Dog();
myDog.bark();  // Output: Woof!

Method vs Function

In Java, functions are always inside classes, and they’re called methods. They define the actions an object or class can perform.


Understanding the main() Method

Sure! Let’s break down the line:

public static void main(String[] args) {
    // Your code goes here
}

This is the entry point of any Java application. Let me explain each part clearly:


1. public

  • Access modifier.

  • Means this method can be accessed from anywhere — even by the Java Virtual Machine (JVM), which runs the program.


2. static

  • Means the method belongs to the class itself, not to any object.

  • So, JVM doesn’t need to create an object to run main() — it just calls it directly.


3. void

  • Return type.

  • Means this method does not return any value.


4. main

  • The name of the method.

  • JVM looks for this exact name as the starting point of your program.


5. String[] args

  • It’s a parameter: an array of Strings.

  • It holds command-line arguments passed when running the program.

Example:

java Main Hello World

args[0] = "Hello"
args[1] = "World"


6. { } (Curly Braces)

  • Everything between {} is the body of the method.

  • You write your logic (statements, loops, etc.) here.

Let’s break it down:

  • public: Accessible from anywhere. Required for the JVM to find and run it.

  • static: Belongs to the class, so JVM can run it without creating an object.

  • void: This method doesn't return any value.

  • main: The method name where execution starts.

  • String[] args: An array of Strings to hold command-line arguments.

Means:

“This is a method named main, visible everywhere (public), it doesn’t belong to an object (static), it returns nothing (void), and it accepts an array of Strings (String[] args) which can receive input from the command line.”

Writing a Simple Java Program

public class Main {
    public static void main(String[] args) {
        System.out.println("Program started");

        int a = 5;
        int b = 10;
        int sum = a + b;
        System.out.println("Sum is: " + sum);

        for (int i = 0; i < 3; i++) {
            System.out.println("Loop i = " + i);
        }

        System.out.println("Command-line args:");
        for (String arg : args) {
            System.out.println(arg);
        }
    }
}

Line-by-Line Explanation

  1. System.out.println("Program started"); – Prints a message to the console.

  2. int a = 5;, int b = 10; – Declare and initialize two integers.

  3. int sum = a + b; – Adds two numbers.

  4. System.out.println("Sum is: " + sum); – Prints the result.

  5. The for loop – Runs 3 times, printing the value of i each time.

  6. The enhanced for loop – Prints any command-line arguments passed to the program.


Memory Management: Stack and Heap

Stack

  • Stores method calls and local variables (like a, b, sum, i).

  • Follows Last In, First Out (LIFO) structure.

  • Cleared when a method finishes execution.

Heap

  • Stores objects and arrays (like String[] args).

  • Managed by Garbage Collector, which removes unused objects automatically.


Garbage Collection

Java handles memory cleanup automatically. If an object is no longer reachable, it's marked for garbage collection:

Dog d = new Dog();
d = null; // Now eligible for garbage collection

Threads in Java

A thread is a separate path of execution. Java supports multithreading using the Thread class or Runnable interface.

class MyThread extends Thread {
    public void run() {
        System.out.println("Running thread");
    }
}

Access Modifiers

ModifierVisibility
publicEverywhere
privateOnly inside the class
protectedSame package or subclass
(default)Same package

Static Methods

public static void main(String[] args)
  • static means main() belongs to the class, not to any object.

  • You don't need to do:

      Main m = new Main();
      m.main(args);
    

    Instead, JVM runs it directly:

      Main.main(args);
    

A static method belongs to the class, not instances (objects):

class Utility {
    static void greet() {
        System.out.println("Hello");
    }
}

Called using Utility.greet();


Return Types

  • void: Returns nothing

  • Other types like int, String, etc., return specific values.

int add(int a, int b) {
    return a + b;
}

Data Types and Wrapper Classes

Primitive Types:

  • int, float, double, char, boolean, long, short, byte

Wrapper Classes:

  • Object versions of primitives: Integer, Float, Double, etc.

  • Useful in collections like ArrayList<Integer>


Loops and Increments

For Loop

for (int i = 0; i < 5; i++) {
    System.out.println(i);
}

i++ vs ++i

int i = 5;
int a = i++;  // a = 5, then i becomes 6
int b = ++i;  // i becomes 7, then b = 7
  • i++ (post-increment): returns old value, then increases

  • ++i (pre-increment): increases first, then returns


Print vs Println

System.out.print("Hello ");
System.out.println("World");
  • print() – prints on the same line

  • println() – prints and then goes to next line


Conclusion

Java might look complex at first, but once you break it down line by line, it starts to make sense. By understanding how memory works, what the main method does, and how data flows in a Java program, you set a solid foundation for more advanced topics like OOP, collections, and multithreading.

Happy coding!

EXTRA -

Generated image

0
Subscribe to my newsletter

Read articles from ADARSH KUMAR PANDEY directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ADARSH KUMAR PANDEY
ADARSH KUMAR PANDEY