File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Very Basic Question .... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Very Basic Question ...." Watch "Very Basic Question ...." New topic
Author

Very Basic Question ....

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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 ?
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Sure:
Congratulations! You just successfully compiled and (hopefully) executed your first Java program! :-)

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Tony,
Yes, I am a begineer in java. Please be a bit serious about this question....and clarify my doubt.
Thanks...
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
OK: What, exactly, is your doubt? I did not see any specific question in your original post. "Shoot again!"
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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...
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20271
    ∞

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!

permaculture Wood Burning Stoves 2.0 - 4-DVD set
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
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
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
or...
<CODE>static public void main (String[] args) </CODE>
or
<CODE>static public void main (String args[]) </CODE>
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Dec 14, 1998
Posts: 20271
    ∞

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
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
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

Joined: Nov 22, 2008
Posts: 18944
Can somebody please give some example showing that main() without public modifier gives problems....
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
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.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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).]
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
murthy
Greenhorn

Joined: Sep 22, 2000
Posts: 2
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.

fdggf
Javix Protocol
Ranch Hand

Joined: Mar 21, 2000
Posts: 57
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!


Write Once , Run Anytime ,Anywhere Forever And Smile Happily.
Javix Protocol
Ranch Hand

Joined: Mar 21, 2000
Posts: 57
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Very Basic Question ....