Results 1 to 5 of 5

Thread: Java keywords public static void main (String args[])

  1. #1
    Join Date
    Feb 2009
    Posts
    5

    Java keywords public static void main (String args[])

    Hello again!

    This is my new question

    Java keywords public static void main(String args[])

    I want to know the meanings of these keywords!

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Java keywords public static void main (String args[])

    The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared.
    In this case, main( ) must be declared as public, since it must be called by code outside of its class when the program is started.
    The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made. The keyword void simply tells the compiler that main( ) does not return a value. As you will see, methods may also return values.
    As stated, main( ) is the method called when a Java application begins. Keep in mind that Java is case-sensitive. Thus, Main is different from main. It is important to understand that the Java compiler will compile classes that do not contain a main( ) method. But the Java interpreter has no way to run these classes. So, if you had typed Main instead of main, the compiler would still compile your program. However, the Java interpreter would report an error because it would be unable to find the main( ) method.
    Any information that you need to pass to a method is received by variables specified within the set of parentheses that follow the name of the method. These variables are called parameters. If there are no parameters required for a given method, you still need to include the empty parentheses. In main( ), there is only one parameter, albeit a complicated one. String args[ ] declares a parameter named args, which is an array of instances of the class String. Objects of type String store character strings. In this case, args receives any command-line arguments present when the program is executed.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Java keywords public static void main (String args[])

    public- main(..) is the first method called by java environment when a program is executed so it has to accessible from java environment. Hence the access specifier has to be public.

    static: Java environment should be able to call this method without creating an instance of the class , so this method must be declared as static.

    void: main does not return anything so the return type must be void

    The argument String indicates the argument type which is given at the command line and arg is an array for string given during command line.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Java keywords public static void main (String args[])

    Access modifiers in java

    Access modifiers specifies who can access them. There are four access modifiers used in java. They are public, private, protected, no modifer (declaring without an access modifer). Using ‘no modifier’ is also sometimes referred as ‘default’ or ‘friendly’ access. Usage of these access modifiers is restricted to two levels.

    I) Class level access modifiers (java classes only)
    Only two access modifiers is allowed, public and no modifier

    * If a class is ‘public’, then it CAN be accessed from ANYWHERE.
    * If a class has ‘no modifer’, then it CAN ONLY be accessed from ’same package’.


    II) Member level access modifiers (java variables and java methods)
    All the four public, private, protected and no modifer is allowed.

    * public and no modifier - the same way as used in class level.
    * private - members CAN ONLY access.
    * protected - CAN be accessed from ’same package’ and a subclass existing in any package can access.

    http://javapapers.com/core-java/acce...-java-explain/

  5. #5
    mitulgolakiya Guest

    Re: Java keywords public static void main (String args[])

    Thanks......
    I was not sure about static keyword in java...

    Because I have only tried static keywords for variables only.....
    I have never used it for methods or functions....

Similar Threads

  1. Differences between int main() and int main(void)
    By BOGUMIL in forum Software Development
    Replies: 5
    Last Post: 10-06-2011, 06:55 AM
  2. Replies: 5
    Last Post: 15-12-2010, 07:18 PM
  3. Why main method is static in java?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 04:20 PM
  4. Java example for static members
    By ScarFace 01 in forum Software Development
    Replies: 6
    Last Post: 05-01-2010, 05:14 AM
  5. java error “javascript:void(null)”
    By Kitaen in forum Windows XP Support
    Replies: 3
    Last Post: 27-07-2007, 05:13 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,286,164.13644 seconds with 17 queries