Your First Java Program — Anatomy of main

প্রথম Java প্রোগ্রাম — main মেথডের শরীর

Read: ~25 min Beginner 5 practice problems Live code runner

1. The Most Famous Line in Java

Every Java program begins with the same magic incantation: public static void main(String[] args). Six tokens, each there for a reason. Understanding them turns Java from a mystery into a very regular language.

Java-র প্রতিটি প্রোগ্রাম একই লাইন দিয়ে শুরু — public static void main(String[] args)। ছয়টি শব্দ, প্রতিটির আলাদা অর্থ। এই লাইন বুঝলেই Java অনেক সহজ হয়ে যাবে।
Main.java
// The canonical first program
class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
        System.out.println("হ্যালো, বিশ্ব!");
    }
}

2. Token by Token

TokenEnglishবাংলায়
class MainDeclares a class named Main. Every executable piece of Java lives inside a class.Main নামে class ঘোষণা। সব Java কোড class-এর ভেতরে থাকে।
publicVisibility: the JVM (external code) is allowed to call this method.JVM এই method কল করতে পারবে।
staticBelongs to the class itself — no object instance needed to call it.object না বানিয়েই class-এর মাধ্যমে কল করা যাবে।
voidReturns nothing.কোনো মান return করে না।
mainThe exact name the JVM looks for as the entry point.JVM ঠিক এই নামের method-ই খুঁজে entry point হিসেবে।
String[] argsAn array of command-line arguments passed by the user.command-line-এ পাঠানো argument-এর array।
public: বাইরে থেকে access দেওয়া যায় কিনা। static: object তৈরি না করেই class-এর সাথে সরাসরি। void: return value নেই। main: JVM-এর নির্দিষ্ট খোঁজার নাম। String[] args: user যা argument পাঠাবে, সেগুলো এখানে array হিসেবে আসবে।

3. Visual Breakdown

public static void main(String[] args) visibility (JVM can call) class-level (no instance) returns nothing exact name JVM looks for array of command-line args Figure 4.1 — public static void main(String[] args) একটু একটু করে।

4. Why class Main (and not public class Main)?

In your local IDE, you'll often see public class Main inside a file called Main.java. Java requires the public class name and filename to match. In this course we use class Main (package-private) because the online sandbox stores your code in a generic prog.java file — a public Main class there would fail to compile.

IntelliJ-এ public class Main লিখলে ফাইলের নামও Main.java-ই হতে হবে। এই কোর্সের online runner ফাইলের নাম দেয় prog.java, তাই আমরা public বাদ দিয়ে শুধু class Main ব্যবহার করি — উভয় জায়গাতেই কাজ করে।
Fine print: only one class per .java file can be public, and its name must match the file. Non-public (package-private) classes have no such restriction.

5. Printing Output — print, println, printf

Main.java
class Main {
    public static void main(String[] args) {
        System.out.print("No newline at end — ");
        System.out.println("this one adds a newline.");
        System.out.printf("Formatted: %s is %d years old.%n", "Arif", 21);
        System.out.printf("Pi ≈ %.3f%n", 3.14159265);
    }
}
print — শেষে newline নেই। println — শেষে newline আছে। printf — format string দিয়ে সুন্দর output। %s=string, %d=int, %.2f=২ দশমিক float, %n=platform newline।

6. Common First-Day Mistakes

❌ Mistakes to avoid

  • Typing Main with different case — Java is case sensitive.
  • Naming the method Main or run — JVM only recognizes main.
  • Forgetting static — JVM cannot call it without an instance.
  • Using System.Out or System.out.Println — check case.
  • Missing semicolons ; at statement ends.

✅ Correct shape

class Main {
  public static void main(String[] args) {
    System.out.println("ok");
  }
}
Java case sensitive — System আর system দুই জিনিস। method-এর নাম ঠিক main হতে হবে। static ভুলে গেলে JVM method call-ই করতে পারবে না। প্রতি statement-এর শেষে ; দিন।

7. What Happens When You Run It

  1. You write Main.java.
  2. javac compiles it to Main.class (bytecode).
  3. You run java Main. The JVM starts up.
  4. The JVM loads Main, looks for public static void main(String[]).
  5. It calls that method, passing your command-line args wrapped in an array.
  6. Your code runs. When main returns, the JVM exits.
৬টি ধাপ — source লেখা → javac দিয়ে compile → java Main দিয়ে run → JVM boot → main method খোঁজা → কোড চালানো ও exit।

8. Vocabulary

TermMeaningবাংলায়
Entry pointThe method where program execution begins.প্রোগ্রাম যেখান থেকে শুরু হয়।
SignatureMethod name + parameter types (and order).method-এর নাম ও parameter-এর ধরন।
StaticClass-level member; no instance required.class-এর সাথে সরাসরি যুক্ত member।
ArgsCommand-line arguments as String[].command-line argument-এর array।
System.outThe standard output stream (a PrintStream).standard output stream।
Case sensitiveUppercase ≠ lowercase in identifiers.বড়-ছোট হাতের অক্ষরে পার্থক্য।

9. Practice Problems

  1. Write a program that prints "Bangladesh" in English and in Bangla on two separate lines.
    "Bangladesh" ইংরেজি ও "বাংলাদেশ" বাংলায় — দুই লাইনে print করুন।
    ✨ Show Answer (উত্তর দেখুন)
    Main.java
    class Main {
        public static void main(String[] args) {
            System.out.println("Bangladesh");
            System.out.println("বাংলাদেশ");
        }
    }
  2. Use printf to print the area of a circle with radius 7 to 4 decimal places.
    radius ৭ একটি বৃত্তের area printf-এর মাধ্যমে ৪ দশমিক পর্যন্ত print করুন।
    ✨ Show Answer (উত্তর দেখুন)
    Main.java
    class Main {
        public static void main(String[] args) {
            double r = 7;
            double area = Math.PI * r * r;
            System.out.printf("Area = %.4f%n", area);
        }
    }
  3. Why must the main method be static? Explain in 2-3 sentences.
    main method-কে কেন static হতে হয়?
    ✨ Show Answer (উত্তর দেখুন)

    Answer: When the JVM first loads a class, no object of it exists yet — a chicken-and-egg problem. A static method belongs to the class itself, so the JVM can invoke it without first constructing an instance. Once your code runs, you can decide whether to instantiate other classes or not.

  4. Print the first command-line argument, or a default message if none was given.
    প্রথম command-line argument প্রিন্ট করুন, না পেলে default message।
    ✨ Show Answer (উত্তর দেখুন)
    Main.java
    class Main {
        public static void main(String[] args) {
            if (args.length > 0) {
                System.out.println("First arg: " + args[0]);
            } else {
                System.out.println("No arguments supplied.");
            }
        }
    }
  5. Fix this broken program (don't run it as-is — describe the bugs, then show the fixed, runnable version):
    Class main {public void Main(String args) {System.Out.println("Hi")}}
    উপরের ভুল প্রোগ্রামটির সব bug খুঁজে, ঠিক করে দেখান।
    ✨ Show Answer (উত্তর দেখুন)

    Bugs: Class should be class (lowercase); method name must be main (lowercase) not Main; missing static; return type missing (should be void); parameter must be String[] args not String args; System.Out → System.out; println is lowercase; missing semicolon.

    Main.java
    class Main {
        public static void main(String[] args) {
            System.out.println("Hi");
        }
    }

Summary — Module 04

public static void main(String[] args) is the entry point. public lets the JVM call it; static means no instance required; void means no return value; main is the name the JVM looks for; String[] args carries the command-line arguments. Java is case sensitive — System.out not System.Out. Use println for a line, printf for formatted output.

public static void main(String[] args) প্রতিটি Java প্রোগ্রামের entry। Case sensitivity ভুলবেন না। print, println, printf তিনটিরই কাজ আলাদা।

Next Module → Primitives, wrapper classes, and the subtle cost of autoboxing.