• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Legal java program?

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Is this a legal Java program?
package pkg;
import java.awt.*;
It compiles fine but doesn't run (complains of missing main method). Does the code snippet constitute a legal Java program? What is a legal Java program? Is it a .java file which compiles or must it compile and as well as run? Thanks again.
Gaia.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
It all depends on what you call a program. If you mean one that will execute, then it must have a top-level class and a properly formed main method to run.
For example:
package pkg;
import java.awt.*; //package and import not necessary
public class MyClass{
public static void main(String args[]){
System.out.println("Hello world!");
}
}
is a valid java program if defined in a file named MyClass.java. It can be run after compiling by using the java MyClass command from the command line. Output will be Hello world!
Does that help?

------------------
Brian Hoff
Sun Certified Programmer for the Java� 2 Platform
 
Gaia Nathan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Brian,
Yes, it does. Actually, the confusion came up when I attempted this question:
Question:
Which of the following are legal Java programs. Select all the correct answer.
A. // The comments come before the package
package pkg;
import java.awt.*;
class C{}
B. package pkg;
import java.awt.*;
class C{}
C. package pkg1;
package pkg2;
import java.awt.*;
class C{}
D. package pkg;
import java.awt.*;
E. import java.awt.*;
class C{}
F. import java.awt.*;
package pkg;
class C {}
Would u agree with the given answer: A, B, D, E
I don't quite agree with D.
Thanks again.
Gaia.
 
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
Hi! Actually D is legal. You can have an empty file and it will still be a legal java file. You only need a main method for an application. A applet does not need a main method. It's the browser that deals with this it's own way. That's why you can compile a program without a main method , but you cannot run it by typeing java yourprog
// Mathias
 
Gaia Nathan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohhh...i see. Thanks Mathias, Brian.
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic