Your First Java Program — Anatomy of main
প্রথম Java প্রোগ্রাম — main মেথডের শরীর
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.
public static void main(String[] args)। ছয়টি শব্দ, প্রতিটির আলাদা অর্থ। এই লাইন বুঝলেই 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
| Token | English | বাংলায় |
|---|---|---|
class Main | Declares a class named Main. Every executable piece of Java lives inside a class. | Main নামে class ঘোষণা। সব Java কোড class-এর ভেতরে থাকে। |
public | Visibility: the JVM (external code) is allowed to call this method. | JVM এই method কল করতে পারবে। |
static | Belongs to the class itself — no object instance needed to call it. | object না বানিয়েই class-এর মাধ্যমে কল করা যাবে। |
void | Returns nothing. | কোনো মান return করে না। |
main | The exact name the JVM looks for as the entry point. | JVM ঠিক এই নামের method-ই খুঁজে entry point হিসেবে। |
String[] args | An array of command-line arguments passed by the user. | command-line-এ পাঠানো argument-এর array। |
3. Visual Breakdown
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.
public class Main লিখলে ফাইলের নামও Main.java-ই হতে হবে। এই কোর্সের online runner ফাইলের নাম দেয় prog.java, তাই আমরা public বাদ দিয়ে শুধু class Main ব্যবহার করি — উভয় জায়গাতেই কাজ করে।
.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
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
Mainwith different case — Java is case sensitive. - Naming the method
Mainorrun— JVM only recognizesmain. - Forgetting
static— JVM cannot call it without an instance. - Using
System.OutorSystem.out.Println— check case. - Missing semicolons
;at statement ends.
✅ Correct shape
class Main {
public static void main(String[] args) {
System.out.println("ok");
}
}
System আর system দুই জিনিস। method-এর নাম ঠিক main হতে হবে। static ভুলে গেলে JVM method call-ই করতে পারবে না। প্রতি statement-এর শেষে ; দিন।
7. What Happens When You Run It
- You write
Main.java. javaccompiles it toMain.class(bytecode).- You run
java Main. The JVM starts up. - The JVM loads
Main, looks forpublic static void main(String[]). - It calls that method, passing your command-line args wrapped in an array.
- Your code runs. When
mainreturns, the JVM exits.
javac দিয়ে compile → java Main দিয়ে run → JVM boot → main method খোঁজা → কোড চালানো ও exit।
8. Vocabulary
| Term | Meaning | বাংলায় |
|---|---|---|
| Entry point | The method where program execution begins. | প্রোগ্রাম যেখান থেকে শুরু হয়। |
| Signature | Method name + parameter types (and order). | method-এর নাম ও parameter-এর ধরন। |
| Static | Class-level member; no instance required. | class-এর সাথে সরাসরি যুক্ত member। |
| Args | Command-line arguments as String[]. | command-line argument-এর array। |
System.out | The standard output stream (a PrintStream). | standard output stream। |
| Case sensitive | Uppercase ≠ lowercase in identifiers. | বড়-ছোট হাতের অক্ষরে পার্থক্য। |
9. Practice Problems
-
Write a program that prints "Bangladesh" in English and in Bangla on two separate lines."Bangladesh" ইংরেজি ও "বাংলাদেশ" বাংলায় — দুই লাইনে print করুন।
✨ Show Answer (উত্তর দেখুন)
Main.javaclass Main { public static void main(String[] args) { System.out.println("Bangladesh"); System.out.println("বাংলাদেশ"); } } -
Use
printfto print the area of a circle with radius 7 to 4 decimal places.radius ৭ একটি বৃত্তের areaprintf-এর মাধ্যমে ৪ দশমিক পর্যন্ত print করুন।✨ Show Answer (উত্তর দেখুন)
Main.javaclass Main { public static void main(String[] args) { double r = 7; double area = Math.PI * r * r; System.out.printf("Area = %.4f%n", area); } } -
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
staticmethod 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. -
Print the first command-line argument, or a default message if none was given.প্রথম command-line argument প্রিন্ট করুন, না পেলে default message।
✨ Show Answer (উত্তর দেখুন)
Main.javaclass 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."); } } } -
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:
Classshould beclass(lowercase); method name must bemain(lowercase) notMain; missingstatic; return type missing (should bevoid); parameter must beString[] argsnotString args;System.Out→System.out;printlnis lowercase; missing semicolon.Main.javaclass 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 তিনটিরই কাজ আলাদা।