Académique Documents
Professionnel Documents
Culture Documents
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.
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.
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.
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
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.