0% found this document useful (0 votes)
4 views22 pages

Lesson 03

This document provides a guide on creating a Java main class using the NetBeans IDE, including steps for creating projects and classes, writing the main method, and using System.out.println for output. It covers essential concepts such as Java classes, packages, and common syntax errors. The document also includes exercises for practical application of the learned concepts.

Uploaded by

wweellddeerr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views22 pages

Lesson 03

This document provides a guide on creating a Java main class using the NetBeans IDE, including steps for creating projects and classes, writing the main method, and using System.out.println for output. It covers essential concepts such as Java classes, packages, and common syntax errors. The document also includes exercises for practical application of the learned concepts.

Uploaded by

wweellddeerr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

3

Creating a Java Main


Class

Copyright © 2019, Oracle and/or its affiliates. All rights reserved.


Objectives

After completing this lesson, you should be able to:


• Use the NetBeans IDE to create and test Java classes
• Write a main method
• Use System.out.println to write a String literal to system output

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3-2
Topics

• Java classes and packages


• The main method

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3-3
Java Classes

A Java class is the building block of a Java application.

ShoppingCart.java Includes code that:


• Allows a customer to add items to the shopping
cart
• Provides visual confirmation to the customer

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3-4
Program Structure

• A class consists of:


– The class name. Class names begin with a capital letter.
– The body of the class surrounded with braces { }

Data (called fields)

Operations (called methods)
• Example:

Java is case-sensitive!

public class Hello {


// fields of the class
// methods
}

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3-5
Java Packages

• A package provides a namespace for the class.


– This is a folder in which the class will be saved.
– The folder name (the package) is used to uniquely identify the class.
– Package names begin with a lowercase letter.
• Example:
Package name
package greeting;

public class Hello {


The class’s unique name
// fields and methods here is: greeting.Hello
}

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3-6
Java IDEs

A Java Integrated Development Environment (IDE) is a type of software that makes it easier
to develop Java applications.
• An IDE provides:
– Syntax checking
– Various automation features
– Runtime environment for testing
• It enables you to organize all your Java resources
and environment settings into a Project.
• Projects contain packages.
• Packages contain files, such as .java.

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3-7
The NetBeans IDE
Code
Project Editor
Navigator

Class
Navigato
r

Program Output

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3-8
Creating a Java Project

1. Select File > New Project.


2. Select Java Application.
3. Name and set the location for the project.
4. Select “Create Main Class” if you want it
done for you automatically.
5. Click Finish.

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3-9
Creating a Java Class

1. Select File > New File.


2. Select your project and choose Java Class.
3. Name the class.
4. Assign a package.
5. Click Finish.

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3 - 10


Exercise 3-1:Creating a New Project and Java Class

In this exercise, you use NetBeans to create a new Java Class.


1. Create a new project called Exercise_03-1.
– Deselect the box to create the main method. You will write the main method yourself
in the next exercise.
2. Create a new Java Class file in this project.
– Class name = ShoppingCart
– Package name =exercise

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3 - 11


Opening an Existing Java Project

If you need to open an existing project in NetBeans, perform the following steps:
1. Select File > Open Project.
2. Navigate to the directory that contains your projects.
3. Select the project file you want. (This file must be unzipped.)
4. Click Open Project.

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3 - 12


Creating a New Java Package

If you ever need to create a new package, perform the following steps in NetBeans:
1. Right-click your project.
2. Select New > Java Package.
3. Name the package.
4. Click Finish.

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3 - 13


Topics

• Java classes and packages


• The main method

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3 - 14


The main Method

• It is a special method that the JVM recognizes as the starting point for every Java
program.
• The syntax is always the same:

public static void main (String[] args) {


// code goes here in the code block
}

• It surrounds entire method body with braces { }.

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3 - 15


A main Class Example
Class name

public class Hello {

public static void main (String[] args) {


// Entry point to the program.
Comments
// Write code here:
System.out.println ("Hello World!");
}
}

main Program
meth output
od
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3 - 16
Output to the Console

• Syntax:
System.out.println (<some string value>);

• Example: String literal

System.out.println ("This is my message.");

Be sure to include the


semicolon

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3 - 17


Avoiding Syntax Errors

• NetBeans will tell you if you have done something wrong.


• Common errors include:
– Unrecognized word (check for case-sensitivity error)
– Missing close quotation mark
– Unmatched brace
– Missing semicolon

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3 - 18


Compiling and Running a Program by Using NetBeans

Save is
equivalent to
javac.
Run is
equivalent
to java.

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3 - 19


Exercise 3-2: Creating a main Method

In this exercise, you manually enter a main method that prints a message to the console.
1. Continue editing Exercise_03-1 or open Exercise_03-2.
2. In the code editor, add the main method structure to the ShoppingCart class.
3. In the code block of the main method, use a System.out.println method to print
“Welcome to the Shopping Cart!”
4. Save your program.
5. Click the Run button to test program.
– Select exercise.ShoppingCart as the main class.

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3 - 20


Quiz Q
Which main method syntax is correct?
a. Public static void main (String[ ] args){ }
b. public Static void Main (String[ ] args){ }
c. public static void main (String ( ) args)[ ]
d. public static void main (String[ ] args){ }

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3 - 21


Summary

In this lesson, you should have learned how to:


• Use the NetBeans IDE to create and test Java classes
• Write a main method
• Use System.out.println to write a String literal to system output

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. 3 - 22

You might also like