• Post Reply Bookmark Topic Watch Topic
  • New Topic

What will happen if I make main() method private instead of public?  RSS feed

 
Prashant Mumbarkar
Greenhorn
Posts: 27
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is if instead of public access I write private in front of main() then what will happen?
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Prashant,

I'd say: try it out and see what happens!

Greetz,
Piet
 
Prashant Mumbarkar
Greenhorn
Posts: 27
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:hi Prashant,

I'd say: try it out and see what happens!

Greetz,
Piet


I tried but my IDE shows error like : Class "ClassName" does not have a main method. But in similar thread folks are saying it works fine on their JVM.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remarkable, since you must have a public static void main() somewhere around, since it is the starting point
of your application. Of course, you can define private main-methods as you see fit, and use these, but again:
your application needs this public main to start.

Well, at least to my knowledge.

Greetz,
Piet
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on what you mean by "work". You can certainly compile it that way. You can write classes that don't have any main() method.

but running it is a different issue. if you are going to do this:

>java <MyClassName>

then MyClassName must have a public static void main (String []) method.
 
Prashant Mumbarkar
Greenhorn
Posts: 27
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:It depends on what you mean by "work". You can certainly compile it that way. You can write classes that don't have any main() method.

but running it is a different issue. if you are going to do this:

>java <MyClassName>

then MyClassName must have a public static void main (String []) method.


I am using NetBeans IDE 7.3 and trying to run the program with private main(). As I'm just beginning to learn such an amazing language and have little exposure to IDE hence don't know how it gets compile & how I can use command line ... It would be great if I get little inputs on that too. Thanks


 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Freds answer is more complete than mine, but here goes.

If you are using NetBeans, and want to create an application, then you do this
by creating a new Project, and give that a name. It has already ticked the option
'Create main class' with the defalut name of the project. Normally, you can leave it that way.

You'll see that NB has created a class for you, with a name equal to your Poject name,
and it has nicely added the 'public static void main(String args[]) {...}' for you.

Now, try changing the word 'public' to 'private', you will notice that NB does not complain, and in fact
you can even compile it (use the menu 'Run' and then option 'Build'. Still no problems).
But now, try to run it, and you see what report you get.

Again: an application can only be run when it knows in which class to start, and in that class
there must be a 'public' main method.

If you create other classes in the project, these classes do not need any main method, private or
public.

Greetz,
Piet
 
Prashant Mumbarkar
Greenhorn
Posts: 27
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Piet for reply.

I went through the steps you explained and I could see what your first paragraph says after that my observations are like below.
Once you create Project then you need to create package or you will be given default package. In that package once you create class then you get syntax like class with {} in file but you do not get default(ready-made) main() method. you should write it. When I tried to use compile option from Run menu sometimes it is enable and sometimes it's not.

My question : can we compile single class or not?

The one file that I could compile main() method with private was successfully compiled. But as you mentioned we can NOT run that program.
Also we can NOT run class file with main() method.

Please share you views on what I saw. Thanks.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are indeed just beginning, then most folk here would say using the IDE is a bad idea. The IDE hides stuff from you. It politely tries to correct some of your mistakes, but doesn't always tell you. Then things go horribly awry when you run your code, and you are left scratching your head.

You should learn to compile from the command line.

so...

A class does not have to have a main method. It can be a 100% valid and useful class without one. Many pre-made classes that come with Java do not have any main method.

Your class can have several main methods. There is nothing special about a method named main - with one exception. You can overload it, you can override it, you can ignore it...but as long as the java is valid (no spelling errors, properly formatted, etc), you can compile it.

RUNNING it is a different story. In my opinion, it doesn't make sense to say "when I run a class". Classes don't run. Methods run. methods call other methods.

When you start up your java program on a command line, you would type something like this:

java MyClass


That tells your operating system to fire up the JVM, to locate the code for the MyClass class, and find the method that has this EXACT signiature:


(ok....you can re-arrange the 'void' and static, and you can name the String array anything, but basically...what I said).

So if you only have a "private static void main (String [] args)", the JVM can't find the method it is looking for, and it bails out, not running anything.

If you have 5 or 10 "main" methods, as long as ONE matches the signature, that is the one that will run.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:(ok....you can re-arrange the 'void' and static, and you can name the String array anything, but basically...what I said)


You cannot re-arrange the void and static keywords, the return type must precede the function identifier directly... what I assumed you meant was that you can re-arrange the access modifier (public) and the non-access modifier (static).

To add to what everyone else said, the main method that must be specified as public static void main(String[] anyValidIdentifier) or static public void main(String[] anyValidIdentifier) is the interface between your application and the java virtual machine... without it no communication can take place between the two, that is how the platform was designed.

To break down what each part mean:

1. The public keyword states that the method is publicly accessible to other code (the virtual machine)
2. The static keyword states that other code can call the method without creating an object of the class as follows NameOfClass.main(stringArray[]) (the virtual machine)
3. The void keyword states that the method returns no value to the calling code (the virtual machine)
4. main is the identifier to the function that the virtual machine looks for... capitalization is important, cannot be Main or MAIN or mAin etc
5. The function's formal parameter list must be a string array (String[]) with any valid identifier.
 
Paul Clapham
Sheriff
Posts: 22843
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:So if you only have a "private static void main (String [] args)", the JVM can't find the method it is looking for, and it bails out, not running anything.


If I recall right, some old versions of Java didn't check whether the method was declared public or not. No doubt that was many years ago, but the web doesn't let people forget anything and so people are forced to go through this kind of trivia for at least ten years after it's obsolete.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:if you are indeed just beginning, then most folk here would say using the IDE is a bad idea. The IDE hides stuff from you. It politely tries to correct some of your mistakes, but doesn't always tell you. Then things go horribly awry when you run your code, and you are left scratching your head.

You should learn to compile from the command line.
(...)

Then I guess I'm not one of most folk here. When I started learning Java, it was just because of the horrors
of having to deal with command line and command line windows, driving me nuts, that made me look for some IDE.
Never regretted it, and the help you get from an IDE, checking your code at typing time, is invaluable.

But this discussion will go on for ever and I don't want to start another one here.

In that package once you create class then you get syntax like class with {} in file but you do not get
default(ready-made) main() method. You should write it

If I start a new project, by clicking the big icon, then up pops a window asking what kind of application
I want to start. You see some categories in the middle panel. In my case, I have the choice between
Java, JavaFX and some more. In the right panel, you see some possible types of application.
When I go for the category 'Java' and choose for 'Java application', and clicking for 'Next', then
you get a window that asks for the name of the project. Then all that's left is to click the 'Finish' button,
and then I have a public class with the 'public static void main(String[] args)' filled in.

But in case this 'main' doesn't appear for whatever reason, yes, then you must type it manually.

Looking at all the replies so far, I get the impression that what I thought was a simple question
turns out to be not so simple to answer.
But I hope it has become clear.

Greetz,
Piet
 
Shiva Gajjala
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prashant Mumbarkar
My question is if instead of public access I write private in front of main() then what will happen?


If you are learning java coding and want to see a class compiling and running , its better if you start practising with a notepad or notepad++ instead of using IDE.
You can declare a class and compile it like this javac <class-name>.java and when you run it like this java <class-name> in the command prompt,then
the JVM will load the class into its memory by using method class.forName(className); and calls static method main() whose parameter is String array like this className.main(String[] str).There are 4 access specifiers - public ,protected,default and private. If you want your any method or class or any member to be accessed from anywhere in the system you need 2 declare it as public. As JVM is installed in your system and you want it to access your main method ,u need 2 declare it as public.
Private members are accessed only within the class .
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:
fred rosenberger wrote:(ok....you can re-arrange the 'void' and static, and you can name the String array anything, but basically...what I said)


You cannot re-arrange the void and static keywords, the return type must precede the function identifier directly... what I assumed you meant was that you can re-arrange the access modifier (public) and the non-access modifier (static).

yes - thanks. once again trying to do too much at once...
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I may suggest I didn't understand and like using IDE either because of having problems as a beginner. So I will suggest this other program that I'm using and its great I have learned so much from this forum and the great people here.

JGrasp

Ensure your Java JDK is updated to the current as well
 
Prashant Mumbarkar
Greenhorn
Posts: 27
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your valuable reply
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!