• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Very Basic Question ....

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to compile (using JDK 1.2.2) the following program and it worked fine.
class HelloWorld {
static void main(String args[]) {
System.out.println("Hello World");
}
}
Can anybody throw some light on this ?
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure:
Congratulations! You just successfully compiled and (hopefully) executed your first Java program! :-)
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony,
Yes, I am a begineer in java. Please be a bit serious about this question....and clarify my doubt.
Thanks...
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK: What, exactly, is your doubt? I did not see any specific question in your original post. "Shoot again!"
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Certification Guide by Phillip Heller says that main() method must be declared public so that the JVM has access to it.
But in the above mentioned program, it does not contain public, even then it executes and prints "Hello World".
Please do consider that a beginner is asking this question.
Thanks...
 
Trailboss
Posts: 23868
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wacky.
My guess is that since you ran the program from the same directory that you made and stored the program, that everything turned out okay.
But! In the future, not putting public there may haunt you!
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried running it creating a package. This too works fine.
This restriction is there only for JVM since only for JVM, it is the entry point. Am I right ?
So, if a question is asked what is the right signature for main() method entry point of an application, what should be the answer in the following.
a) public static void main(String args[])
b) static void main(String args[])
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Paul. And that's not all the issues you'll come across if you always compile and/or use the "default" package which is the directory in which you compile.



For example, having the main() method in the same class allows you to access private (static) variables from the class. But if the main() method is outside of that class, it will not be able to access the private variable... I mention that because it gave me a headache once :-)



You'll see as you progress in your Java studies... We are here to help.
And, by the way, it's:
<CODE>public static void main(String args[])</CODE> or <CODE>public static void main(String[] args)</CODE>
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or...
<CODE>static public void main (String[] args) </CODE>
or
<CODE>static public void main (String args[]) </CODE>
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony,
I am so sorry to say that your explanation is not very clear. Please find some more time to be more elaborate.
thanks....
 
paul wheaton
Trailboss
Posts: 23868
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think if the question comes up on the test, you better put "public" in there. That's the way sun has trained us to do it. In all the books. If the VM is running it without "public" in there, it could be considered a bug in the VM!
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ShivaPrasad: "I am so sorry to say that your explanation is not very clear. Please find some more time to be more elaborate."
It's my fault. I should have stuck to your question. Just know the correct way to specify the main() method in a class so that it can be executed. There are specified above.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody please give some example showing that main() without public modifier gives problems....
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, sure:
In directory c:\tony I have the following
<PRE>
//File Xclass.java
package tony
public class Xclass {
static void main(String[] args) { // <--- NOTICE NO public

//Do my stuff...
system.out.println("Result is...");
}
}
</PRE>
But if I try to execute the class from directory c:\tony2 and I type:
<CODE>java Xclass</CODE>
or more precisely:
<CODE>java tony.Xclass</CODE>
I will get an error to the effect that java could not find a <CODE>public static void main(String[] args)</CODE> because that main() has only package access and not public as required.
So for it to work always, the method has to be public.
I don't have the time to test this (although I am pretty confident in the answer) so I'm open for counter-arguments :-)
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thing has only come up with JDK1.2
Previous versions like JDK1.1 give an error.
/* I would appreciate if someone could check with JDK1.1
I read about this explaination in a posting somewhere
Thanks
*/
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Tony,
i tried executing the code given by you.i ran it from c:\tony and also from c:\tony2. but it worked!! so i would like to know how did it work even without the public modifier.how is it able to access the main method outside the package? please explain.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Madurai,
I guess Maha can answer on behalf of Tony. What you say is correct. It runs from any directory. The discussion of 'Making the main(String[] args) method to be package/protected/private level and still it runs' is brought up already here. The newer versions of JDK does in fact allows this. Someone posted that earlier version of JDK didn't. I am using jdk1.2.2 and it runs from any directory. I also think that we should go according to JLS. What if our present code is run in future using different implementation which does in fact goes as per JLS?
The JLS says this . The entry point of any application which is the main method has to be public static void main(String[] someName) { }. What tony told was as per the JLS spec.
Other than this Java behaves as told in JLS. The compiler checks All access levels (public/protected/package/private..) at the compile time itself whether the accessed member of the other class is in fact really obeys the rules defined for access levels. I tried to give you an example. Compile this. You will get compile time error. It would't even pass the compile check.
Compile like this. Your current dir has to be in classPath.
javac -d . Point.java
javac -d . PlusPoint.java

regds
maha anna
<pre>

//Point.java
package points;
public class Point {
public static void public_static_printMe() {
System.out.println("I am from points.Point.public_static_printMe()");
}
protected static void protected_static_printMe() {
System.out.println("I am from points.Point.protected_static_printMe()");
}
static void package_static_printMe() {
System.out.println("I am from points.Point.package_static_printMe()");
}
private static void private_static_printMe() {
System.out.println("I am from points.Point.private_static_printMe()");
}
}
//PlusPoint.java
package morepoints;
import points.Point;
public class PlusPoint {
public static void main(String[] args) {
Point.public_static_printMe();
Point.protected_static_printMe();
Point.package_static_printMe();
Point.private_static_printMe();
}
}

</pre>
[This message has been edited by maha anna (edited April 18, 2000).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Essence - malformed main() function, without the public
modifier compiles and runs fine.
If you use older versions of JDK, it might throw an
error, jdk 1.2 compiles and runs fine.
I found out this has been logged as a BUG and SUN has
no intentions of fixing it. They say this is not an
issue.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code will compile and run if you declare it as
private static void main(String args[]) in jdk1.2 onwards.
But in jdk1.2 the code won't work if you declare like that.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The entry code will compile and run if you declare it as
private static void main(String args[])or final static void main(String args[])or static final void main(String args[])also in jdk1.2.1 onwards.

------------------
javix!
 
Javix Protocol
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The entry code will compile and run if you declare it as
private static void main(String args[])or final static void main(String args[])or static final void main(String args[])also in jdk1.2.1 onwards.

------------------
javix!
 
It will give me the powers of the gods. Not bad for a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic