• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

Beginner Java Help – Understanding Classes, Objects, and Main Method

 
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone 👋,

I’m new to Java and just started learning the basics. I understand the syntax a little, but I’m still confused about some core concepts.

I have the following doubts:

What is the exact role of a class in Java?

How does an object work in real-time programs?

Why is the main() method declared as public static void main(String[] args)?

When should we use static vs non-static methods?

Can someone explain this simple program step by step?


I’ve read tutorials, but practical explanations or real-world examples would really help me understand better.

Thanks in advance for your guidance!

-Sabari Ganesh R
 
Marshal
Posts: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags to your code; doesn't it look better now

I shall only answer a few of your questions. I would have thought those questions would have been covered in your teaching.
When should something be static? If you are writing object‑oriented programs, then things static are atypical and unusual, and the norm is for everything to be instance type (i.e. not static).  I dislike the term non‑static because it makes it seem there is something normal about things being static. Things instance belong to the object and things static belong to the class. You create an instance from a class (that is another description of an object) and use its instance methods and use its instance fields via its instance methods.
Sometimes you want to call a method before there are any instances. Maybe you want a method to create instances, so you might not have any instances beforehand, and such a method would have to be static. The main() method has to be called before there are any instances, so it is static. It is also public so it can be called from outside the current package, and it doesn't return anything, so it is marked void. The Java® Language Specification (=JLS) is very strict about that, but those requirements might change in future releases. I think the name main comes from a similar “function” in ealier languages like C and C++.
Sometimes you want constants, like this number. In that case you only want one copy of the constant. A feature of things static is that there is always one of them, whether you have one instance of the class, 2, 3, 4, 5 … 1,000,000 instances, or even no instances. So you don't need any instances and you usually make constants static. If you go through that link and look at the remainder of that page, you won't find any way to create an instance of that class.
You will also notice that everything else in that class is marked static. It is what we often call a “utility class,” which means it exists to provide services for other code. All its methods take information, do some calculations, and produce a result. None of them ever records any of that information for future reference, so it never needs an instance. So they had to make all methods static too. I have seen a very dubious classification of methods, here, which suggests when it would be a good idea to make a method static. If you try that on the Math class, you will find the methods (I think all of them) come out as number 1368.
Challenge: work out how the Math class could have been designed differently so as to have only instance methods.
 
Campbell Ritchie
Marshal
Posts: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

An hour ago, I wrote:. . . I shall only answer a few of your questions. . . .

Maybe I'll come back later and give you some more answers; maybe somebody else will give you answers.
 
Bartender
Posts: 11226
91
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
### 1) What’s the exact role of a **class** in Java?

A **class** is a *blueprint/type definition* that describes:

* what **data** something has (fields),
* what **behavior** it has (methods),
* and the rules/invariants around it (constructors, encapsulation, etc.).

At runtime, a `Class` also exists as metadata loaded by the JVM (via a class loader), used for things like method dispatch, reflection, and verification—but conceptually: **a class defines what objects of that type will look like and do.**

---

### 2) How does an **object** work in real-time programs?

An **object** is a runtime instance of a class, living in memory (heap, typically). In “real-time” or responsive programs (games, GUIs, trading systems, embedded, etc.), objects:

* hold **state** that changes over time (position, balance, UI model, etc.),
* expose methods that **act on that state**,
* interact concurrently (threads) or sequentially (event loops).

The real-time part doesn’t change what an object *is*—it changes what you must care about:

* **allocation/GC pauses** (too many short-lived objects can create latency spikes),
* **thread-safety** (multiple threads touching the same object needs coordination),
* **predictability** (favor reusing objects, preallocating, using queues, etc.).

---

### 3) Why is `main()` declared as `public static void main(String[] args)`?

Because the JVM looks for *exactly* a method with this signature as the entry point.

* **`public`**: the JVM must be allowed to call it from outside the class.
* **`static`**: the JVM can call it **without creating an object** first.
* **`void`**: it doesn’t return a value to the JVM as a program result.
* **`main`**: the specific name the JVM searches for.
* **`String[] args`**: command-line arguments passed in, e.g. `java Hello one two`.

Common variants are also accepted by the JVM, like `public static void main(String... args)`.

---

### 4) When should we use **static** vs **non-static** methods?

Use **non-static (instance) methods** when the behavior depends on an object’s state.

Use **static methods** when the behavior:

* does **not** need per-object state,
* is a general utility,
* belongs to the class as a whole (factory, helper, constants-related),
* or is required for entry points / callbacks.

Rule of thumb:

* If it conceptually acts on **“this object”**, make it instance.
* If it acts on **inputs only** and doesn’t need object identity/state, static is fine.

---

### 5) Step-by-step: what happens in your program?

Your code:

Here’s what happens:

1. **Compile**

* You run: `javac Hello.java`
* The compiler outputs `Hello.class` (bytecode).

2. **Start the JVM**

* You run: `java Hello`
* The JVM starts, sets up memory areas, loads core classes.

3. **Load your class**

* The JVM loads `Hello.class` via a class loader.
* It verifies the bytecode (safety checks), then links it.

4. **Find the entry point**

* The JVM searches `Hello` for a method named `main` with the correct signature.
* It finds `public static void main(String[] args)`.

5. **Call `main`**

* No `new Hello()` happens (because `main` is static).
* The JVM invokes `Hello.main(...)`.
* `args` is an empty array if you didn’t pass command-line parameters.

6. **Execute the print**

* `System` is a standard Java class.
* `System.out` is a `PrintStream` connected to your console.
* `println("Hello World")` writes text plus a newline.

7. **Program ends**

* `main` returns.
* Since there are no non-daemon threads left running, the JVM exits.
 
Campbell Ritchie
Marshal
Posts: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A few hours ago, I wrote:. . . Maybe I'll come back later . . .

I see Carey has given you lots of answers
There is more about the main method in the Java™ Tutorials. I think I have given you the right section.
 
Ranch Hand
Posts: 44
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sabari,
After some answers you got, I want to advise something.
First you need to understand a few other things and learn others after it.
You can start with elementary programming(identifiers,variables, etc) , selections, loops, methods, arrays then later you can understand what you asked.
After you learn these then you can understand static,non-static very well.
Don't rush while learning(jump or ignoring basic) and accept what you see on the show(ide) as it is, it will help you.
What you are trying to learn is connected to OOP(Object oriented programming)(classes) which is  a bit difficult(not impossible) to learn before understanding basic.
A great resources to learn from are books(if you need recommendations you can ask me), if you don't like books you can learn from websites(freecodecamp,w3schools etc).
Just keep learning and don't stop.
I hope this helps a little.
reply
    Bookmark Topic Watch Topic
  • New Topic