1.05k likes | 1.06k Views
Learn how to write and execute a Java program. Understand String concatenation, escape sequences, compiling, and debugging. Overview of creating, declaring, and running Java classes. Step-by-step guidance provided.
E N D
Writing a Java program. How to send output to the command line console. Learn how String concatenation works. Learn about escape sequences. Learn how to compile, debug, and execute a Java program. Objectives:
Top Down Design • Start JCreator. • Create a new file called “Lab01.java”. • Save the new file in your Lab01 folder.
Creating A Java Class File (cont…) Class names should always begin with an uppercase letter.
Creating A Java Class File (cont…) Click this button to set the location.
public class Lab01 { } Declaring A Java Class This class can be used by other classes!
public class Lab01 { } Declaring A Java Class The keyword class defines this file as a Java class.
public class Lab01 { } Declaring A Java Class User defined name. Class names should always begin with an uppercase letter.
public class Lab01 { } Declaring A Java Class The body of every class begins and ends with a set of curly brackets.
In Java, you need to have a method named main in at least one class. This method must appear within a class, but it can be any class. A class containing a main method is a program. Every program has a main method but not every class is a program. The main Method
public class Lab01 { public static void main(String[ ] args) { } } The main Method (cont…) Curly brackets are used to define block statements. Methods, like classes, are block statements and must begin and end with a set of curly brackets.
public class Lab01 { public static void main(String[ ] args) { } } The main Method (cont…) The main method is static.
Console Output In Java, console output is achieved by calling System.out.printor System.out.println. • The data to be output is given as an argument in parentheses. System.out.println(“Blackjack”); • Every invocation of println ends a line of output. Blackjack _
println Versus print The print method is like println, except that it does not end a line • With println, the next output goes on a new line • With print, the next output goes on the same line
public class Lab01 { public static void main(String[ ] args) { System.out.println(“Main Method!”); } } The main Method (cont…)
public class Lab01 { public static void main(String[ ] args) { System.out.println(“Main Method!”); } public void output() { System.out.println(“Hello World”); } } Calling Methods output is not static
public class Lab01 { public static void main(String[] args) { System.out.println(“Main Method!”); } public void output() { System.out.println(“Hello World”); } } Calling methods (cont…) Run the program
public class Lab01 { public static void main(String[ ] args) { System.out.println(“Main Method!”); } public void output() { System.out.println(“Hello World”); } } Calling methods (cont…) output did not execute
public class Lab01 { public static void main(String[ ] args) { System.out.println(“Main Method!”); } public void output() { System.out.println(“Hello World”); } } Calling methods (cont…)
public class Lab01 { public static void main(String[ ] args) { } public void output() { System.out.println(“Hello World”); } } Calling methods (cont…) How do we get from here
public class Lab01 { public static void main(String[ ] args) { } public void output() { System.out.println(“Hello World”); } } Calling methods (cont…) How do we get from here to there
public class Lab01 { public static void main(String[ ] args) { output(); } public void output() { System.out.println(“Hello World”); } } Calling methods (cont…) We need to call the method output()
public class Lab01 { public static void main(String[ ] args) { output(); } public void output() { System.out.println(“Hello World”); } } Calling methods (cont…) Build the program
error: non-static method output() cannot be referenced from a static context
Calling methods (cont…) Double click on the first line of the error.
Calling methods (cont…) The editor will send the cursor to the line that contains the error.