Link to home
Start Free TrialLog in
Avatar of nature
nature

asked on

Is main is mandatory

Hello,

   I came across a java code which reads as below in this there is no main part and still the code gets compiled and works.  My question is that is main is mandatory for each and every program if not on what logic does the compiler will interpret those programs which has no main.  And also can anyone tell me how to create array of handle for objects with a simple example.

The code goes here
-------------------
class gg {
  private gg() {}
  // (1) Allow creation via static method:
  public static gg makeSoup() {
    return new gg();
  }
  // (2) Create a static object and
  // return a reference upon request.
  // (The "Singleton" pattern):
  private static gg ps1 = new gg();
  public static gg access() {
    return ps1;
  }
  public void f() {}
}

class Sandwich { // Uses Lunch
  void f() { new Lunch(); }
}

// Only one public class allowed per file:
public class Lunch {
  void test() {
    // Can't do this! Private constructor:
    //! gg priv1 = new gg();
    gg priv2 = gg.makeSoup();
    Sandwich f1 = new Sandwich();
    Soup.access().f();
  }
}
-----------------------------------------

Thank u in advance,
nature
Avatar of diakov
diakov

One
public static void main(String[] args)
method member of a class is needed per application.
the difference between applets and applications is that in case of applets the browser instantiates your class derived from Applet, and in case of application the JVM finds the main declaration and runs it and only it. In there you have to allocate your GUI class instances, initialize threads if necesarry and many others.

Cheers,
  Nik
Avatar of nature

ASKER

Sorry nik i am not getting the answer correctly could u put it more explanatory as i am new to this subject.

Hope i will get a clear answer

Cheers
Nature
nature: then how do you make this program run? I mean, what command do you give? (As you can see, I don't believe it's possible, Nik's right.) Don't forget that an application/applet uses classes (the objects) that don't need to have a main or init, but they can't be run standalone.
It looks like it would compile OK. But I don't see how it could run, and, even if it did, I don't see how you could tell. It doesn't show anything on the screen, or print anything out, or write a file or anything.

ASKER CERTIFIED SOLUTION
Avatar of Staplehead
Staplehead

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nature

ASKER

I accept the comment and the answers.

Comment for vendrig
--------------------

     I ran this application thro jpad which has a icon in it.

Comment for imaldris
---------------------

  No imaldris i compiled the code and i ran it.  I was astonished that it prints the output even without main.  Indeed that was the juice of this question, if the compiler would show me an error stating an "error without main", i could not have this doubt at all.

Thank u all for ur efforts,
Nature
u need a main.. however in java the main is also an object
u need a main.. however in java the main is also an object
no: in the case of applets, you do not need a function main. further, in the case of applications, main is not an object, but a static method of a class.  often, no objects of this class are ever instantiated.

conceptually, it's odd, but it keeps consistancy in the programming model.