d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > [java] General Question About Classes
Add Reply New Topic New Poll
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
Apr 26 2013 03:03am
I understand classes and objects like this:
A class is a description for a type of object. Objects are unique instances of a class.

This definition works well for any class without a main() method, but I get confused when trying to apply it to the "main class" of a program. What's the object here?
Could you say that a running program is a unique instance (object) of the class with the main() method?
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Apr 26 2013 06:14am
In Java, main is a static method. This means the method is part of its class and not part of objects.

For example, Robots are objects. They are defined by classes, which are like factories for the creation of objects. In terms of our metaphor for robot programming, a static method is like the instructions read to robots by the helicopter pilot, not the instructions known by the robots themselves.
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
Apr 26 2013 09:19am
Thanks.

1. Why has the main() method to be part of a class though, if there's no object to be described by the class?
2. What IS the class with the main method? In terms of the metaphor, it's not a factory for the creation of anything.

This post was edited by tt_toby on Apr 26 2013 09:45am
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Apr 26 2013 10:13am
Quote (tt_toby @ Apr 26 2013 11:19am)
Thanks.

1. Why has the main() method to be part of a class though, if there's no object to be described by the class?
2. What IS the class with the main method? In terms of the metaphor, it's not a factory for the creation of anything.


The main method the starting point of your application. Every application has to have a main method.
When you're creating a debug configuration, you have to start somewhere and it's a class.
For example, if you run java from command line, you have to pass it a class (the main class). And it finds the main method in that class to use it as the starting point.

The class with the main method is referred to as the "main class" and usually has only the main method in it.

This post was edited by labatymo on Apr 26 2013 10:14am
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Apr 26 2013 10:28am
Quote (labatymo @ Apr 26 2013 11:13am)
The main method the starting point of your application. Every application has to have a main method.
When you're creating a debug configuration, you have to start somewhere and it's a class.
For example, if you run java from command line, you have to pass it a class (the main class). And it finds the main method in that class to use it as the starting point.

The class with the main method is referred to as the "main class" and usually has only the main method in it.


That's not what he's asking.

Static methods and fields on classes define behavior that is relevant to the class but doesn't rely on any instance-specific state. The Java psv main method is a bit of a special case.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll