Top
Previous Next
Elements of the Java Platform CS 161 - Java

A Better First Java program

Source: examples/MyName.java



 
/** a Java class to demonstrate simple Java principles.
*/
 
public class MyName {
 
public static void main(String[] args) {
 
// Make a MyName object ...
 
MyName anObject = new MyName();
 
// Assign its name from the command line argument
 
anObject.name = args[0];
 
// ... and ask it to to identify itself
 
System.out.println( anObject.toString() );
 
// The following is only needed for running the program from
// within the Javaedit application on Windows
try {
byte[] line = new byte[80];
System.out.println("press enter key to end");
System.in.read(line);
}
catch (java.io.IOException e) {
// ignored
}
}
 
// The rest of this relates to MyName "objects"
 
// each MyName object will remember who you tell it is using
// this "instance variable" named "name".
 
String name;
 
// This will allow MyName objects to identify themselves asked.
 
public String toString() {
return "Hello, I'm a MyName object and my name is " + name;
}
 
}


Top
Previous Next
jwd