Author Topic:   Very Basic Question ....
ShivaPrasad
ranch hand
posted January 30, 2000 07:04 PM             
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
sheriff
posted January 30, 2000 07:09 PM             
Sure:

Congratulations! You just successfully compiled and (hopefully) executed your first Java program! :-)

ShivaPrasad
ranch hand
posted January 30, 2000 07:39 PM             
Tony,
Yes, I am a begineer in java. Please be a bit serious about this question....and clarify my doubt.
Thanks...

Tony Alicea
sheriff
posted January 30, 2000 08:00 PM             
OK: What, exactly, is your doubt? I did not see any specific question in your original post. "Shoot again!"

ShivaPrasad
ranch hand
posted January 30, 2000 08:11 PM             
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
sheriff
posted January 30, 2000 08:22 PM             
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!

ShivaPrasad
ranch hand
posted January 30, 2000 08:30 PM             
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
sheriff
posted January 30, 2000 08:33 PM             
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:

public static void main(String args[]) or public static void main(String[] args)

Tony Alicea
sheriff
posted January 30, 2000 08:36 PM             
or...

static public void main (String[] args)

or

static public void main (String args[])

ShivaPrasad
ranch hand
posted January 30, 2000 08:51 PM             
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
sheriff
posted January 30, 2000 08:54 PM             
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
sheriff
posted January 30, 2000 09:33 PM             
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.

ShivaPrasad
ranch hand
posted January 31, 2000 01:36 AM             
Can somebody please give some example showing that main() without public modifier gives problems....

Tony Alicea
sheriff
posted January 31, 2000 08:39 AM             
OK, sure:

In directory c:\tony I have the following


//File Xclass.java
package tony
public class Xclass {

static void main(String[] args) { // <--- NOTICE NO public

//Do my stuff...
system.out.println("Result is...");
}
}


But if I try to execute the class from directory c:\tony2 and I type:

java Xclass
or more precisely:
java tony.Xclass
I will get an error to the effect that java could not find a public static void main(String[] args) 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 :-)

Anubhav
unregistered
posted January 31, 2000 10:33 AM           
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
*/

madhuri
unregistered
posted April 18, 2000 03:59 PM           
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
bartender
posted April 18, 2000 09:18 PM             
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



//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();
}
}

[This message has been edited by maha anna (edited April 18, 2000).]

Ajith Kallambella
ranch hand
posted April 19, 2000 12:56 PM             
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.

murthy
unregistered
posted April 23, 2000 03:14 PM           
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.

Javix Protocol
ranch hand
posted April 24, 2000 02:23 AM             
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
posted April 24, 2000 02:24 AM             
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!

|