Solved

Is main is mandatory

Posted on 1998-10-05
9
274 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:nature
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 8

Expert Comment

by:diakov
ID: 1225415
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
0
 

Author Comment

by:nature
ID: 1225416
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
0
 
LVL 2

Expert Comment

by:vendrig
ID: 1225417
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.
0
 
LVL 16

Expert Comment

by:imladris
ID: 1225418
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.

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Accepted Solution

by:
Staplehead earned 50 total points
ID: 1225419
nature,

a java application must have a main() function somewhere in it.  in fact, it should (!) be static, so that you don't have to create an instance of the class in order to run it.

a java applet will not have a main() function that you write.  instead, you write code into functions defined in class Applet, the class that your particular applet has to extend.

the code you posted may compile; after all, each .java file will be its own class (private classes notwithstanding), but that doesn't mean that they *do* anything until you write code to instantiate them and do things with them.

think of it this way: your code is like a car in a parking lot... it works, but it doesn't "do" anything until someone comes along with a key, start it up, and use it.  make sense?
0
 

Author Comment

by:nature
ID: 1225420
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
0
 

Expert Comment

by:arun_kumar_ks
ID: 1225421
u need a main.. however in java the main is also an object
0
 

Expert Comment

by:arun_kumar_ks
ID: 1225422
u need a main.. however in java the main is also an object
0
 
LVL 1

Expert Comment

by:Staplehead
ID: 1225423
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strCount chalenge 3 50
XML Paring  Error - Premature end of file. 7 56
topping3 challenge 14 50
recursion example 16 68
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now