Use of public access modifier in main method

I want to know what is the significance of public access modifier with main (String args[]) method. Like generally we write
public static void main(String args[])
But if we write
private static void main(String args[])
OR
protected static void main(String args[])
then also its working properly.............
then what is the use of public keyword..........................
Regards
Ajay Pratap Singh

then what is the use of public keyword..........................Convention, I believe. And I think newer versions of the JVM require it.
P.S. Relax a bit on the punctuation overuse. Many folks around here find that a bit irritating, and you probably don't intend to send that kind of a message. Cheers!

Similar Messages

  • What is the use of passing String[] args in main() method?

    what is the use of passing String[] args in main() method?
    Is there any specific use for this ?

    actually my sir asked me the same question & I gave
    the same reply as given by you........but he further
    asked what is the use of this also??
    ie accepting cmd line args at runtime??is there any
    specific purpose ??Apart from the one you just mentioned? No

  • Access Specififer for main Method

    Dear all,
    In JDK1.2.2 if I give private access modifier to the main method,its working.
    But in JDK 1.3,it displays "Main method not public".
    what could be the reason?

    Please don't post the same question multiple times. It's annoying.
    Please don't post the same question multiple times. It's annoying.
    Please don't post the same question multiple times. It's annoying.
    Please don't post the same question multiple times. It's annoying.
    http://forum.java.sun.com/thread.jsp?thread=341247&forum=33&message=1404079
    http://forum.java.sun.com/thread.jsp?thread=341187&forum=33&message=1403858
    http://forum.java.sun.com/thread.jsp?thread=341186&forum=33&message=1403857
    Especially within the same Forum.

  • Protected and public access modifier - exact difference

    Hi everybody,
    Please help me to give an exact difference between protected access modifier and public
    Protected: Classes with in the same package as well as subclasses can access.
    Public : Classes outside the current class can access while declaring public.
    My doubt is that what is the practical difference between public and protected.
    I searched the net , but the information was not satisfactory to solve my confusion.
    Thanks & regards
    Parvathy

    Obviously doubt = lacks the ability to think for
    oneself.When one person raises a doubt, that does n't mean that he lacks the ability to think . He/She might have thought about it and if he didnt get a proper clarification then it will be posted in the forum.
    In such cases please dont discourage others who are ready to answer the subject.Without knowing the actual intention , i request you please dont reply like this

  • Access modifiers ignored !?!

    Hi
    please, have a look at the code below and let me know your opinions
    // ------------- source file 1 ------------------------
    package pckg1;
    public class Dog {
         final public static void main (String[] args) {
              pckg2.Puppy puppy = new pckg2.Puppy("Spot");
              System.out.println(puppy.getName());
    // ------------- source file 2 --------------------
    package pckg2;
    class Puppy {
         String name;
         public Puppy(String name) {
              this.name = name;
         String getName() {return name;}
    Each class belongs to a different package. Obviously if you compile Puppy first and attempt compiling Dog the compiler will complain about several things (class Puppy not being public, method getName() not being public, etc...).
    Now,
    if you change the Puppy class so that the necessary bits are public, re-compile Puppy and then Dog it will work.
    When you subsequently go back to Puppy and remove the added public access modifiers and re-compile JUST the Puppy class and try running Dog IT WILL STILL WORK AS IF EVERYTHING IN THE PUPPY CLASS WERE PUBLIC.
    I don't thing this is right. If the access modifiers cannot be relied on then the whole system is flawed.
    PS: Running on W2K, the behaviour observed on JRE 1.2.2_007, 1.3.0_02 as well as on 1.4.0-beta-b65.
    Cheers
    Ales Krestan

    Thanks for the response, but I hold different views on the matter. Although performance surely is of a great importance, it should not come at the expense of consistency.
    A few more thoughts on the same:
    1/ my understanding is that it is responsibility of the class loader to decide what to do in such cases during the resolution case. Apparently the application class loader of SUN's JVM ignores it and so does the extension class loader of the same JVM.
    2/ surprisingly (or rather expectedly) when attempting to load both classes (with the dodged version of Puppy) using the system class loader the JVM throws java.lang.IllegalAccessError.
    3) the same trick cannot be done with an interface. If you try to dodge an interface using the same principle and try to load it using the application class loader JVM will refuse to run (or load) the class that uses the dodged interface.
    4/ the class loader DOES other things, so why not to check the access modifiers as well. It should check if the class can be instantiated, for example. See the section 2.17.3 Linking: Verification, Preparation, and Resolution of the JVM specification.

  • Usage of default access modifier

    Some programmers don't use the default access modifier (package level). Is there a specific reason? If so, what is it? Or is it a good to use default access modifier?
    Thank you,
    Srikanth

    Some prefer to grant or limit access to an object by the interface they expose. In this case all methods are either public or private. Access is restricted based on the fact that only the appropriate code is given references of certain interface types and if code does not have the right interface, they can not access the method.
    Its a different style, but I use it myself quite a bit. This way I never am concerned over if it should be public,private, protected or default. The choice is simpler. But sometimes it can make you create interfaces for really simple things which I am not uptight enough to do...

  • Public main method in a non-public class

    Hi,
    It is said that main method in a java class must be public so that it can be invoked by JVM. This works even if the class itself has default modifier.
    But while programming, any method (be it public) of a class with default access modifier from one package can't be accessed by classes from other packages. For example,
    1. Consider class A in package a.
    package a;
    class A {
    public void methA() {}
    2. Now consider class B in pakage b.
    package b;
    class B {
    void methB() {
    // Here it can't access class A and its method methA()
    So how does JVM invoke the main method of a class with default access modifier. Assume that the class is in some package a.b.c
    Regards,
    Amit

    Hi,
    Maybe this can also be beneficial for you.
    Standalone applications don't necessarily have init and start methods. They must have a public static void main routine to start things off. The main routine typically creates some object of the master class. It can't use any non-static variables or methods in the class with the implied this, since there is no this object. It typically quickly returns. Then the event loop processing starts. Even the static main method must live inside some public class, or at least in the top class, the first class and the class for whom the source file is named. There are no such thing as standalone static methods in Java. The signature of the main class must look exactly like this. Cut and paste it to be sure:

  • Access Modifiers Effect on Static Method Hiding Question

    I am studying for my SCJP exam and have come across a question that I do not understand and have not yet found a satisfactory explanation.
    Given:
    package staticExtend;
    public class A{
         private String runNow(){
              return "High";
         static class B extends A{
              public String runNow(){
                   return "Low";
         public static void main(String args[]){
              A[] a=new B[]{new B(),new C()};
              for(A aa:a)
                   System.out.print(aa.runNow()+" ");
    class C extends A.B{
         public String runNow(){
              return "Out";
    }The output is "High High". The explanation in the sample exam from ExamLab was that because the runNow() method in A was private that only an explicit cast to the B class would invoke the runNow() method in B. I have verified that that is the case, but am not clear on how the runNow() method being declared static in B and how the private access modifier in A results in this behaviour.
    Any additional explanation would be greatly appreciated.
    Thanks in advance.
    -- Ryan

    Ryan_Chapin wrote:
    OK, so since runNow() in A is private the compiler determines that regardless of the available methods in any of it's sub classes that since we declared the original array reference as "A" that it will invoke the runNow() in A. It's also due to the fact that the invocation came from within A. You would have gotten a compile time error if you tried to place the code in the main method in another class.
    >
    My mistake about the second part that you mention. You are correct. runNow() in B is NOT static, but the class is static. I guess that was the red herring in this question I don't see how that is related. I actually think that the "red herring" was what I described above. The fact that the code was placed in A, and that private methods can't be overridden.
    and the fact that the class itself is static has nothing to do with the behaviour that is being illustrated. Is that correct?Correct

  • Static main(String args[]) access modifiers possible

    main(String args[]) can have private as access modifier?

    hi,
    yes of course this is possible, because main is at the end only a method like each other. But you have to know, that you cannot use this method from outside. so no application will start, if the main() of the Main-class has a private as modifier.
    You can use this class as an applet.
    So be carefull doing this
    regards

  • Class extending a Frame with main method in it - how do I use the methods?

    Howdy.
    Having a problem.. I want my "main" class, ie. the class with the main method in it to extend a Frame class.. because the main method is static, I can't use Frame's methods.
    What's a trick to get around this ? I am making a deliberate design decision to extend the Frame class, because I thought it was.. well.. better. I could always just create a Frame object and utilise it.. but from little things I've read and seen it's better to extend it.
    When I use Forte to make a Frame program it sets up a constructor/etc. for the main class and then it does something akin to this (the class is called MazeGenerator) :
    public static void main(String args[]) {
    new MazeGenerator().show();
    What does "new MazeGenerator().show();" do when it's not being associated to an object handle ??
    Ta.
    - Scutt.

    Create an instance of the frame in main then the constructor can control it. Or you could add an init method or something:
    class MyFrame extends Frame {
    public static void main (String [] args) {
    MyFrame frame = new MyFrame();
    frame.init();
    You don't necessarily have to have a reference to an Object in order to create one. All the work can be done in the constructor (something I'm not really fond of however)

  • Calling main method in another class using command line arguements

    Hi
    My program need to use 4 strings command line arguments
    entered in Project properties/Run/Application Parameters
    java programming for beginners // arguments
    The program needs to call main in the second class 4 times using argument 1 the first call, argument 2 the second call on so on.
    So the output wil be
    java
    programming
    for
    beginners
    import java.lang.*;
    public class First extends Second{
      public static void main(String[] args) {
        for(int i = 0; i < args.length; i++){
           Second.main(args); // Error I think
    import java.lang.*;
    public class Second {
    public static void main(String[] args){
    System.out.println(args[0]);
    "First.java": Error #: 300 : method main(java.lang.String) not found in class Second at line 6, column 15
    I am only a beginner with little knowledge of java so can the code be as basic as possible

    Your style looks quite bad for starters..... Hows
    this://import java.lang.*; /* NOT NEEDED */
    public class First extends Second{
    public First(String s) {
    super(s);
    public static void main(String[] args) {
    for(int i = 0; i < args.length; i++){
    new First(s);
    public class Second {
    public Second(String s)
    System.out.println(s);
    NOT NEEDED:
    public static void main(String[] args){
    System.out.println(args[0]);
    }My question to you: Do you understand why my code
    works? (does it do what you want?)I think since this is some kind of lesson, the OP have to implement some way to use the main method of the Second class as it is, that is, with String[] args. I think this lesson is interesting exactly because of this requirenment. But, anyway, I don�t know, that is just my assumption...

  • Should we use main method in JCO

    1/
    Should we use main method in JCO.. Connections does not seem to be simple java programes... so why should we use them??? just for the sake of running the programme?
    2/ And the below mentiond programme.. there is nothing called Connect1. Still the that class is created ..
    do not you thik it has to be <b>TutorialConnect1</b> object and constructor,
    import com.sap.mw.jco.*;
    public class TutorialConnect1 extends Object {
       JCO.Client mConnection;
       public Connect1() {
         try {
           // Change the logon information to your own system/user
           mConnection =
              JCO.createClient("001", // SAP client
                "<userid>", // userid
                "****", // password
                null, // language
                "<hostname>", // application server host name
                "00"); // system number
           mConnection.connect();
           System.out.println(mConnection.getAttributes());
           mConnection.disconnect();
        catch (Exception ex) {
          ex.printStackTrace();
          System.exit(1);
      public static void main (String args[]) {
        Connect1 app = new Connect1();

    Hi
    The main method is used in the learning stages to know how the program is executing.In real time scenarios we do not use the main method.If you use a main method it is just like a stand alone program to know the basics.
    In your program the class name and constructor name is different
    Make sure both are same or otherwise create an object with default constructor given by jvm and then call a method
    Thanks
    kalyan

  • How to print a string with out using main method??

    how to print a string with out using main method??
    if we can tell me with example?
    thanks in adavance
    raja

    Hi,
    actually there's none. The UITableView class needs to "know" the tableView's height so either it can calc <number of rows> times <row height> or it has to sum the height of each single row.
    If the row which needs to be of different size always is the first or the last row you can user a tableHeader or tableFoot view.
    Regards
    Dirk

  • I am opening a public access/community TV station soon. We are thinking of using Final Cut X for our staff

    I am opening a public access/community TV station very soon. We are thinking of using Final Cut X for our staff and community producers. I am curious if there are any special discounts for multiple purchases of computers, but also for Final Cut X software? Does anyone reading have any experience from a public access TV perspective?

    Hi SCaryPictures,
    I run a PEG station, where I have used FCP7, AvidExpressPro, Adobe CS6/CC, and FCPX - I am approaching 2 years with my FCPX experience. I'd be happy to engage with you in a private email thread about my experience (I value my access to the people on this forum, and I don't want the Apple staffers to take offence.) Good luck founding your station!
    Eric D - [email protected]
    CoralVision (City of Coralville, IA)

  • I have admin access to the main router in our LAN, so how can i telnet or get access to other LAN members in LAN without using third party software?

    I have admin access to the main router in our LAN, so how can i telnet or get access to other LAN members in LAN without using third party software?
    its linksys3500 router and  i login as admin using the gateway address in address bar..
    i want to access the c drive of my colleague in same subnet in same office and i know his ip address.but he not configured telnet accept request.so without it how can i open his telnet port and access him

    Duplicate post. 

Maybe you are looking for

  • BAPI for goods moment is not working in loop

    Dear ABAPers, If i am using BAPI ( for MB1n transabtion for goods movement type 309 )with in loop first one is working fine and remaining is not working. If i run individually all r working fine. I am sending cdoe as below. Pl hava look on that.   LO

  • I bought a song on my phone but it won't play on my PC

    This isn't the first time that this is happened... I buy a song through the iTunes app on my iPhone 5C and when I open iTunes on my PC, the song shows up in my Library but won't play and the name is gray and has the exclamtion mark in a circle next t

  • Connect display to imac

    I just purchased a new Imac and need to connect my apple dispaly monitor so I can use both simultaneously.

  • Being able to Save a document

    I created a "fill in" document for my employees; however, when they fill in the document (ie. check boxes, etc.), they are not able to save as a new file, with the inputted information. My employees only have Reader. Is there something I can do when

  • Connecting and sending data to web service

    Hi, I have to implement the functionality in PDF document to connect with the web service and send the form data in xml format on Click event of a button inside the form. During the initial development and testing I have found that document can only