Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: basic question

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default core java questions

    how many types of inputting method in java?can you explain with examples?


  2. #2
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post basic question

    what do you mean by
    public static void main(string[] args)
    {
    }

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: basic question

    The questions you're asking aren't really how this works. In fact, I'm going to merge them.

    The answer to both is: what do you think? What have you tried? What did google tell you?

    Please see the link in my signature on asking questions the smart way before you post again.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: basic question

    The tutorial is a good place to look for how to do things.
    For example for methods: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: basic question

    okk i try it..

  6. #6
    Member
    Join Date
    Jun 2014
    Posts
    33
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: basic question

    I've also stared at the ugly (forgive me Java heads) snippet of code that pops up in every single code example:
    public static void main(string[] args) {}
    Although I've started reading the tutorials over at the Oracle site, skipping all of the exercises and what-not, I'm still not confident I understand what that set of code does. I of course realize that it all will make perfect sense once I understand Java at some level. The fact that no matter what I need to code my program will always start with this... thing, is just annoying.

    This is my understanding of the line of code thus far:
    main()
    This is the meat and potatoes of the expression, as this defines the "main" method of a class. Apparently all "classes" have main methos, that's just how the language works.
    {}
    The "body" of the method goes within these brackets.
    string[] args
    Both of these elements are actually concatenated, as the latter names the (internal) variable containing the arguments (or parameters) that the main method takes as input. The former defines that variable as being an "array" comprising of "string type" values. I'm guessing any number of values, even if I also read that data structures always need to be pre-defined. Dunno what's gives.
    void
    This keyword means that the main method will not "return" any value once called. Apparently main methods never do, albeit other methods of the same class might.
    public static
    These two are basically needed in order not make the main method be "private" or "dynamic". Because that would be bad and thus not compute. This is where I lack any deeper understanding.

    Now, how wrong am I?
    Last edited by Baldyr; June 30th, 2014 at 12:38 PM.

  7. #7
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: basic question

    First of all, it would be good if you started your own thread instead of hijacking the thread of other people. Thats really not the nice way.
    But I will try to help you out as good as I can.

    Quote Originally Posted by Baldyr View Post
    main()
    This is the meat and potatoes of the expression, as this defines the "main" method of a class. Apparently all "classes" have main methos, that's just how the language works.
    Nope.
    A main method is not needed at all, you can have no main method in your entire program.
    But usually its still in use because it defines an entry-point into your application.
    When you application is started it will usually start at a main method.

    Quote Originally Posted by Baldyr View Post
    {}
    The "body" of the method goes within these brackets.
    Thats correct.
    The body of every method goes within such brackets.

    Quote Originally Posted by Baldyr View Post
    string[] args
    Both of these elements are actually concatenated, as the latter names the (internal) variable containing the arguments (or parameters) that the main method takes as input. The former defines that variable as being an "array" comprising of "string type" values. I'm guessing any number of values, even if I also read that data structures always need to be pre-defined. Dunno what's gives.
    This is the parameter list for the method. A parameter list is a list of touples divided by commata.
    Each touple consists of a type and a name for a parameter. In this case the method only has a single parameter, its name is args and its type is an array of String objects.
    You can have as many parameters for a method as you like, even 0. But you have to define a type and a name for each one of them.

    By the way, parameter is what you define in the header of your method. But when you actually pass a value in as a parameter its called an argument.

    Quote Originally Posted by Baldyr View Post
    void
    This keyword means that the main method will not "return" any value once called. Apparently main methods never do, albeit other methods of the same class might.
    Correct.
    Main methods never return anything because they are the entry-point into your program. When the main method is over your program will usually be exited. (unless you create other threads that is)

    Quote Originally Posted by Baldyr View Post
    public static
    These two are basically needed in order not make the main method be "private" or "dynamic". Because that would be bad and thus not compute. This is where I lack any deeper understanding.
    The first one is visibility, there are 4 types of visibility in java:
    public - the method can be assessed from anywhere and anybody
    private - the method can only be assessed from within the class itself (or inner / annonymous classes).
    protected - the method can be assessed from within this class and all sub-classes.
    package private - the method can be assessed from anywhere within the same package. To declare a method package private just dont write anything in front of it.

    The static means that this method does not belong to an object but to an entire class. The main method must be static because before your program is started there will not be any objects.


    For further information you should really read some tutorials or the java specification.

  8. The Following 2 Users Say Thank You to Cornix For This Useful Post:

    Ada Lovelace (June 30th, 2014), Baldyr (June 30th, 2014)

  9. #8
    Member
    Join Date
    Jun 2014
    Posts
    33
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: basic question

    Quote Originally Posted by Cornix View Post
    First of all, it would be good if you started your own thread instead of hijacking the thread of other people. Thats really not the nice way.
    I did not realize that was in fact what I was doing. Since no one else was about to answer the original poster I though I would give it a try. I succeeded in making you, good sir, clarify things for us newbies. So, thank you and sorry for the inconvenience.

    Quote Originally Posted by Cornix View Post
    A main method is not needed at all, you can have no main method in your entire program.
    But usually its still in use because it defines an entry-point into your application.
    When you application is started it will usually start at a main method.
    Well, that's interesting. I wonder why tutorials start with making newbies write that main method anyways, like it really was the one and only way to do it. I'm guessing this is what also made the original poster scratch his or her head. At least we know this much now!

    Quote Originally Posted by Cornix View Post
    This is the parameter list for the method. A parameter list is a list of touples divided by commata.
    Each touple consists of a type and a name for a parameter. In this case the method only has a single parameter, its name is args and its type is an array of String objects.
    You can have as many parameters for a method as you like, even 0. But you have to define a type and a name for each one of them.
    So... you really don't need to put in that definition for the args tuple then, if you don't have any parameters? Seems like waste of code, but perhaps it's usually included for some clarity?

    Quote Originally Posted by Cornix View Post
    By the way, parameter is what you define in the header of your method. But when you actually pass a value in as a parameter its called an argument.
    Ah, I thought that parameters are just what arguments are called with methods (in contrast with mere functions). I stand corrected, then.

    Quote Originally Posted by Cornix View Post
    Main methods never return anything because they are the entry-point into your program. When the main method is over your program will usually be exited. (unless you create other threads that is)
    Since I struggle to grasp this thing - could you give an (unusual) example of where you would want a main method to return some value? Could a program, for example, return a true/false boolean if it managed to complete whatever task it set out to do?

    Quote Originally Posted by Cornix View Post
    The first one is visibility, there are 4 types of visibility in java:
    public - the method can be assessed from anywhere and anybody
    private - the method can only be assessed from within the class itself (or inner / annonymous classes).
    protected - the method can be assessed from within this class and all sub-classes.
    package private - the method can be assessed from anywhere within the same package. To declare a method package private just dont write anything in front of it.
    Since this redundancy of code bothers me, wouldn't it be more convenient if "public" was the "default" type of method, and not "package private"? But I'm guessing it would be even more unconvenient to put those bits into every class method then...

    Quote Originally Posted by Cornix View Post
    The static means that this method does not belong to an object but to an entire class. The main method must be static because before your program is started there will not be any objects.
    But if there can be no exceptions to this, why not make the "static" state of a main method implied? Sure, it would be good form to put it there for clarity, but the code would still compute the same, since it's a main method and all...

    Quote Originally Posted by Cornix View Post
    For further information you should really read some tutorials or the java specification.
    As I said, I'm currently reading the tutorials and aim to read some more. This is just where I'm at right now. Thank you for your time and effort in helping a newbie out.

    For me learning a language isn't merely to accept the way things work, but to understand why they work that way and not some other way. If I know what things are, what they do and why I'm putting them into my code - then I can feel like I can think the language, at some level. (That feeling when you realize that you were dreaming in code...)

  10. #9
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: basic question

    Take a bow Cornix That explaination was better than books and lectures. Seriously, really good dude

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: basic question

    why they work that way
    Often it's because there was an arbitrary decision made by the programmer. Somewhere back in history a programmer decided that the method to start the execution of a program should be named main.
    Currently the java program looks for a public, static, void method named main that takes a String array as the place to start the execution of a class when you enter: java TheClassName

    static makes sense because there isn't an instance of the class yet.

    The String array is a way to pass args to the class when it starts:
    java TheClassName the args here
    would set the String array passed to main() to {"the", "args", here"}
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: basic question

    Quote Originally Posted by Baldyr View Post
    Well, that's interesting. I wonder why tutorials start with making newbies write that main method anyways, like it really was the one and only way to do it. I'm guessing this is what also made the original poster scratch his or her head. At least we know this much now!
    Probably because otherwise you would hardly be able to test your code.
    There are ways to start a java program without a main method, but its not the usual way. The main method is the default entry-point and I guess that is the reason why newbies are teached to start with a main method.

    Quote Originally Posted by Baldyr View Post
    So... you really don't need to put in that definition for the args tuple then, if you don't have any parameters? Seems like waste of code, but perhaps it's usually included for some clarity?
    If you want to use your main method as the entry-point you need to. The arguments that are passed to the method will be the command line input. All java programs are started from a command line, sometimes the command line is hidden by an executable or something else, but its still there. The args array will contain each command line option as an element.
    If you leave this parameter out the main method can no longer be used as an entry-point because the command line options could not be passed anymore.

    Quote Originally Posted by Baldyr View Post
    Ah, I thought that parameters are just what arguments are called with methods (in contrast with mere functions). I stand corrected, then.
    There are no "functions" in java. You write methods, these take parameters but get arguments passed to them.

    Quote Originally Posted by Baldyr View Post
    Since I struggle to grasp this thing - could you give an (unusual) example of where you would want a main method to return some value? Could a program, for example, return a true/false boolean if it managed to complete whatever task it set out to do?
    You can write any kind of method you like and name it main, that is not forbidden. But then you cant use it as an entry-point to your program anymore. Once your main method returns something it is not a "real" main method anymore but instead just a regular method that just happens to be called "main".

    You can also not return anything since java programs are always command line programs. You can write to the output stream though; this is usually done via System.out.println(...).

    Quote Originally Posted by Baldyr View Post
    Since this redundancy of code bothers me, wouldn't it be more convenient if "public" was the "default" type of method, and not "package private"? But I'm guessing it would be even more unconvenient to put those bits into every class method then...
    This is left over from the very early start of java. The java gurus would probably want to change it too, but that would brake backwards compatibility which is holy to java. So once a decision was made it will never be undone.

    Quote Originally Posted by Baldyr View Post
    But if there can be no exceptions to this, why not make the "static" state of a main method implied? Sure, it would be good form to put it there for clarity, but the code would still compute the same, since it's a main method and all...
    Because you can write a main method which is not static, but then it will not be an entry-point for your program anymore but a simple method which just happens to be called "main".

  13. #12
    Member
    Join Date
    Jun 2014
    Posts
    33
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: basic question

    Quote Originally Posted by Norm View Post
    The String array is a way to pass args to the class when it starts:
    java TheClassName the args here
    would set the String array passed to main() to {"the", "args", here"}
    I follow. Your example would require the main method to take three parameters, and only these parameters, right? So the main method wouldn't accept two or four or none parameters, then.

    Quote Originally Posted by Cornix View Post
    If you want to use your main method as the entry-point you need to. The arguments that are passed to the method will be the command line input. All java programs are started from a command line, sometimes the command line is hidden by an executable or something else, but its still there. The args array will contain each command line option as an element.
    If you leave this parameter out the main method can no longer be used as an entry-point because the command line options could not be passed anymore.
    But if my program (-s main method) doesn't require or accept any command line options - what then? Do I still need to define a tuple of strings in the constructor? (I realize that the args name as such is arbitrary though.)

    Quote Originally Posted by Cornix View Post
    There are no "functions" in java. You write methods, these take parameters but get arguments passed to them.
    So this makes Java a OOP language only then. I was kinda wondering about this.

    Quote Originally Posted by Cornix View Post
    Once your main method returns something it is not a "real" main method anymore but instead just a regular method that just happens to be called "main".

    You can also not return anything since java programs are always command line programs.
    That sorta makes a lot of sense, because you of course mean that once the program exits back to the command prompt, there isn't any process left to pick up that return value. Check.

    Quote Originally Posted by Cornix View Post
    Because you can write a main method which is not static, but then it will not be an entry-point for your program anymore but a simple method which just happens to be called "main".
    The only thing left to do is to accept this fact. Thank you, again.

  14. #13
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: basic question

    Quote Originally Posted by Baldyr View Post
    I follow. Your example would require the main method to take three parameters, and only these parameters, right? So the main method wouldn't accept two or four or none parameters, then.
    Quote Originally Posted by Baldyr View Post
    But if my program (-s main method) doesn't require or accept any command line options - what then? Do I still need to define a tuple of strings in the constructor? (I realize that the args name as such is arbitrary though.)
    First of all, you have no control over how many command line arguments your program gets passed on start up. The user can type in anything he/she wants.
    Thats why the parameter args is an Array of Strings. This array could contain any number of elements, even 0 if no command line arguments have been inputted by the user.

    The main method does always look the same way if you want to use it as the entry-point, you never change the number of parameters or their types.

    Imagine a main method like this:

    class Foo {
    	public static void main(String[] args) {
    		for (String s : args) {
    			System.out.println(s);
    		}
    	}
    }
    This method would simply write back every argument that was passed to it.

    Now this program could be started from the command line like this:
    java Foo a b c d
    And the output would be:
    a
    b
    c
    d

    But if you start the program like this:
    java Foo
    The output is just empty, because the array args contains no elements.
    Last edited by Cornix; June 30th, 2014 at 03:14 PM.

  15. The Following User Says Thank You to Cornix For This Useful Post:

    Baldyr (June 30th, 2014)

  16. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: basic question

    java Foo.java
    BTW .java looks like a source file extension.
    If the Foo class is not in a package, the command line would be:
    java Foo
    If you don't understand my answer, don't ignore it, ask a question.

  17. #15
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: basic question

    Quote Originally Posted by Norm View Post
    BTW .java looks like a source file extension.
    If the Foo class is not in a package, the command line would be:
    java Foo
    Yes, right, of course. Stupid IDE's are making me stupid.

  18. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: basic question

    Your example would require the main method to take three parameters,
    No.
    String[] args defines args as a variable that can refer/point to ANY sized single array.
    For example:
    String[] args = {"a", "B"};
    args = new String[4];
    args = new String[] {"a"};
    On each line args is assigned a reference to a different sized array.
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    Baldyr (June 30th, 2014)

  20. #17
    Member
    Join Date
    Jun 2014
    Posts
    33
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: basic question

    Oh, now I see. The length of the args tuple is set by the actual number of parameters passed, not by the main method definition. Understanding how these things work makes it easier for me to understand what it is I'm doing, instead of simply copy-pasting some main method definition template because I've come to accept that this is the only way to do it.

  21. #18
    Member
    Join Date
    Jun 2014
    Posts
    33
    My Mood
    Inspired
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: basic question

    For anyone who might care, I pretty much read the tutorials on this site in one sitting and am finally starting to make sense of things. Particularly why Java works the way it does, which was the thing I was trying to grasp. I'd recommend the tutorial for anyone getting into Java at entry-level!

    The next step would probably be to install JDK and JEdit and start coding and getting used to the syntax and such, then...

Similar Threads

  1. Basic Java Question
    By chaoticcircuit in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 3rd, 2014, 12:18 AM
  2. Rather Basic Question
    By ashl7 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: March 6th, 2013, 03:20 AM
  3. Basic Question Need Help
    By Graser in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 21st, 2012, 10:27 AM
  4. basic java question
    By jim213mm in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2012, 01:30 PM
  5. Very new to Java basic question
    By loofy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 23rd, 2011, 06:21 PM