Vous êtes sur la page 1sur 6

Introduction to Classes

Fundamentals of Classes
Introduction
In the previous two lessons, to use a variable, we were declaring it using one of the known data types. For example, we could use an integer to declare a variable that represented the number of bedrooms of a house. Here is an example: public class Exercise { public static void main(String[] args) { int bedrooms = 3; } } As opposed to a simple variable, you can use one or more variables to create a more complete or complex object. Instead of only one file, you can create a program with many of them. Each file can contain different instructions that, when put together, can create an application as complete as possible.

Practical Learning: Introducing Classes


1. Start NetBeans 2. Create a New Project as a Java Application and name it DepartmentStore1 3. Execute the application to see the result

Creating a Class
A class is a technique of using one or a group of variables to be used as a foundation for a more detailed variable. To create a class, you start with the class keyword followed by a name and its body delimited by curly brackets. Here is an example of a class called House: class House { } A class is created in a code file. As such, you can include it in the first file of your project. Here is an example: class House { } public class Exercise { public static void main(String[] args) { int bedrooms = 3; } } You can also create a class in its own file. To create a class in NetBeans: On the main menu, click File -> New File... On the File toolbar, click the New File button

In the New File dialog box, in the Categories list, click Java. In the File Types list, click Java

Class:

After specifying the name, click Finish. A new file named after the class with the .java extension would be added to your project. When a project is made of various files, each file is represented by a tab in the top section of the Code Editor. Therefore, to access a file, you can click its tab.

Practical Learning: Introducing Classes


1. To create a new class, on the main menu, click File -> New File... In the New File dialog box, in the Categories list, click Java (it should be selected already). In the File Types list, click Java Class (it should be selected already) 2. Click Next 3. In the next page of the wizard, specify the name of the class as DepartmentStore 4. In the New Java Class page of the wizard, click Finish

Declaring a Variable of a Class Type


Like any normal variable, to use a class in your program, you can first declare a variable for it. Like the variables we introduced in the previous lesson, to declare a variable of a class, you can use its name followed by a name for the variable. For example, to declare a variable of the above House class, you could type the following: class House { } public class Main { public static void main(String[] args) { House property; } } The variables we have declared so far are called value variables. This is because such variables of primitive types hold their value. You can use another type of variable. This time, when you declare the variable, its name does not hold the value of the variable; it holds a reference to the address where the actual variable is stored in memory. This reference type is the kind used to declare a variable for a class.

To use a variable as reference, you must initialize it using an operator called new. Here is an example: class House { } public class Main { public static void main(String[] args) { House property = new House(); } } You can also first declare the variable. Then, on another line, you can allocate memory for it using the new operator. Here is an example: class House { } public class Main { public static void main(String[] args) { House property; property = new House(); } } In Java, as well as Visual Basic, if you create a class in any of the files that belong to the same project, the class is made available to all other files of the same project.

Sharing a Class
When creating a class, if you want it to be accessible by code in other files, precede the class keyword with public when creating it. If the class keyword is preceded by public, the class must be created in its own file.

Class' Member Variables


Introduction
Consider a class named House: public class House { } The section between the curly brackets, { and }, of a class is referred to as its body. In the body of a class, you can create a list of the parts that make up the class. Each of these parts must be a complete variable with a name and a data type. For example, here are the characteristics that make up a house, declared as the parts of the above class and each declared as a variable: public class House { String propertyNumber; char propertyType; byte Stories; int bedrooms; double Value; } The variables declared in the body of a class are referred to as its member variables. In Java, these member variables are called fields. The fields can be any type we have seen in the previous lesson. When creating a class, it is your job to decide what your object is made of.

Practical Learning: Introducing Class Members


1. Change the DepartmentStore class as follows: package departmentstore1; public class DepartmentStore { long itemNumber; char category; String itemName; double unitPrice; } 2. Save the file

Initializing an Object
Introduction
After declaring an instance of a class, you can access each of its members and assign it the desired value. Here is an example:

public class House { long propertyNumber; String propertyType; byte Stories; public int Bedrooms; double MarketValue; }

public class Main { public static void main(String[] args) { House property = new House(); property.propertyNumber = 283795; property.propertyType = "Single Family"; property.Bedrooms = 4; property.MarketValue = 652880; } } Once a member variable has been initialized, you can use the period operator to access it and retrieve its value: public class Main { public static void main(String[] args) { House property = new House(); property.propertyNumber = 283795; property.propertyType = "Single Family"; property.Bedrooms = 4; property.MarketValue = 652880; System.out.println("=//= Altair Realty =//="); System.out.println("Properties Inventory"); ; System.out.println("Property #: " + property.propertyNumber); System.out.println("Property Type: " + property.propertyType); System.out.println("Bedrooms: " + property.Bedrooms); System.out.println("Market Value: " + property.MarketValue);

} } This would produce: =//= Altair Realty =//= Properties Inventory Property #: 283795 Property Type: Single Family Bedrooms: 4 Market Value: 652880.0

Practical Learning: Using a Class' Fields


1. To access the main file, click the Main.java tab 2. Change main() as follows: package departmentstore1; public class Main { public static void main(String[] args) { DepartmentStore dptStore = new DepartmentStore(); dptStore.itemNumber = 437876; dptStore.category = 'W'; dptStore.itemName = "Scoop Neck Dress"; dptStore.unitPrice = 148.00D; System.out.println("Department Store"); System.out.println("Stock #: " + dptStore.itemNumber); System.out.println("Category: " + dptStore.category); System.out.println("Name: " + dptStore.itemName); System.out.println("Unit Price: " + dptStore.unitPrice); } } 3. Execute the application. This would produce: Department Store Stock #: 437876 Category: W Name: Scoop Neck Dress Unit Price: 148.00 4.

The Methods of a Class


Introduction

When you create a class, the fields are meant to describe it. For an example of a class named House, such as number of bedrooms or its market value, are used to describe it. Besides the characteristics used to describe can also perform actions or assignments. An action performed by a class is called a method. A method is simply code that takes care of a particular detail for the functionality of the class. To create a method, you specify its n follows the rules we defined for variables. The name of a method is followed by parentheses.

A method's job is to carry a specific assignment within a program. As such, it could provide a value once the ass been carried. In some cases, a method must produce a result. If it doesn't, then it is considered void. The t that a method can provide (or return) is written on the left side of the method name. If the method doesn result, type void to its left. The assignment that a method carries is included between an opening curly brack

closing curly bracket "}". Here is an example: public class House { long propertyNumber; String propertyType; byte Stories; public int Bedrooms; double MarketValue; void Display() { } } The most regularly used method of a Java program is called main.

After creating a method, in its body delimited by its curly brackets, you can define the desired behavior. For e can write the member variables in the parentheses of System.out.print() or System.out.println(). Here are e public class House { long propertyNumber; String propertyType; byte Stories; public int Bedrooms; double MarketValue; void Display() { System.out.println("=//= Altair Realty =//="); System.out.println("Properties Inventory"); System.out.println("Property Type: " + propertyType); System.out.println("Bedrooms: " + Bedrooms); } } In the same way, you can create as many methods as you want in a class.

Vous aimerez peut-être aussi