This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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.
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic