Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

New to Java

TenLeftFingersTenLeftFingers Member Posts: 74
I used to use C++ and now I am learning Java.
Why is the main "inside" a class in an application?
Also, why is there no main in an applet?

It's always the little things :)
Thanks,
tenLeftFingers

Comments

  • moomoo Member Posts: 147
    : I used to use C++ and now I am learning Java.
    : Why is the main "inside" a class in an application?
    : Also, why is there no main in an applet?
    :
    : It's always the little things :)
    : Thanks,
    : tenLeftFingers

    Hi,

    This is quite simple, it's Java standard. Java applications [red]must[/red] implement the main method, to be able to be run. But I wonder, this is similar to c++, isn't it? In Applets you must implement the start method (i guess ;-)). There're three methods [b]init()[/b], [b]start()[/b] and [b]stop()[/b]. The 'init' method is called when the browser initializes the Applet, the 'start' method is invoked when the browser is starting the applet and I think I don't have to explain what the 'stop' method is for ;-). I hope I've been a little help to you.

    Greetings moo

  • TenLeftFingersTenLeftFingers Member Posts: 74
    Thanks,
    I get the applet part, but as for the application,
    in C++ main is a stand alone function, but in Java ( from the examples I have seen ) main is nested inside a class which is saved as a .java file.

    Is this because the file has to be save with the same name as the class?
    How do I decide which class to put my main in if I have more than one class file?

    Thanks again!
    TanLeftFingers
  • moomoo Member Posts: 147
    ...
    : Is this because the file has to be save with the same name as the class?

    Exactly. If you write a class called 'HelloWorld' you'll have to save it as 'HelloWorld.java'.

    : How do I decide which class to put my main in if I have more than one class file?
    :
    : Thanks again!
    : TanLeftFingers
    :

    This depends on your application design. You'll have to decide which class is the controller class of your application.
    The following example will work (not exactly like c/c++ but let me see ;-) )

    the HelloWorld class (HelloWorld.java)
    [code]
    public class HelloWorld {
    public void printMessage() {
    System.out.println("Hello World");
    }
    }
    [/code]

    the Main class (Main.java)
    [code]
    public class Main {
    public static void main(String[] args) {
    HelloWorld myHelloWorld = new HelloWorld();
    //call the hello world method....
    myHelloWorld.printMessage();
    }
    }
    [/code]

    Note! You don't have to implement the main method to write a valid java class. But you will have to, to write a valid executeable java class.
    You would call it as follows:
    [code]
    java Main
    [/code]

    This will work! calling [b]java HelloWorld[/b] will produce an error like 'Could not find main method in class'

    Oki, that's it, I hope I didn't explain too much and you are already sleeping at this point :))))

    Greetings moo
  • chamsterchamster Member Posts: 662
    : How do I decide which class to put my main in if I have more than one class file?

    You pick virtually any class and put your main method in there. That would cause a lot of confusion so the most of people (that i know) creates a class called Main and in there puts only one method - main.

    That way you always know that you should execute a program by
    [code]
    java Main
    [/code]




    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • chamsterchamster Member Posts: 662
    Also - if you're developing for "normal" people you may create a class called "RunMe" or something like that, instead of "Main". That way the only requirement you and your users have to face is being able to read which is pretty common :-)



    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • TenLeftFingersTenLeftFingers Member Posts: 74
    Thanks to you both,
    It's all making sense now =@)

  • chamsterchamster Member Posts: 662
    Soon enough it will stop again. Then it starts again. THen it stops... And so on. We call that learning process :-)

    I've been into Java a lot the last 3 years including teaching at a local university. Still i get to discover new things that don't make sence. Then they start to. Then they stop. Then... Well, you get the point.



    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • chamsterchamster Member Posts: 662
    Byt the way - if you're familiar with programming (as you said you come from C++) you have an enormous advantage since you can express what you want using C/C++ and then simply ask how to convert that "thought" into Java.

    I believe i've seen a C++/Java translator somewhere (it might have been C#/Java).



    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • Andre YoungAndre Young USAMember Posts: 0

    ________ \ http://forcoder.org \ free ebooks and video tutorials about { Visual Basic, Delphi, C, Objective-C, PHP, PL/SQL, Java, Scratch, Python, Assembly, Ruby, R, C++, JavaScript, Perl, MATLAB, Go, C#, Swift, Visual Basic .NET Rust, COBOL, LabVIEW, Kotlin, Apex, Ada, Crystal, Awk, Transact-SQL, Erlang, Julia, Logo, F#, D, SAS, Clojure, Scheme, ML, Scala, Lua, Hack, FoxPro, Dart, Fortran, Lisp, Bash, ABAP, Alice, Prolog, VBScript } ___________

Sign In or Register to comment.