• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

init() & main()

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everybody,
I'm just started Java thru complete reference, i would like to know whether can we have both main() and init() function in a class, and that class should be extended by what class?
i would appreciate any quick reply,
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by gururaj sirdesai:
Hello Everybody,
I'm just started Java thru complete reference, i would like to know whether can we have both main() and init() function in a class, and that class should be extended by what class?
i would appreciate any quick reply,


 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can have both main() and init()functions in a class. You need <pre>
public class myClass{
public static void main(){
...
}
}
</pre>
for any application to begin; init() is optional and is called only if you call it.
If you build an applet,
<pre>
public class myClass extends java.applet.Applet{
...
}
</pre>
you need to write and init() method to override the init() method provided by java.applet.Applet to initialize your class; main() is optional and is called only if you call it.
Marilyn
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay Marylin...as far as being legal or not legal is concerned , you have clearified it..but can you please explain a little more..having a static main method in the code wouldn't it be a problem if we write an applet that has init method(mendatory).
Thanks...
Nasir
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It shouldn't be a problem. If you run an applet, main() would just never be called. Alternatively, you could just put a call to main() in your init() method and initialize everything inside main().
Marilyn
reply
    Bookmark Topic Watch Topic
  • New Topic