Download presentation
Presentation is loading. Please wait.
1
Tonga Institute of Higher Education
Creating Classes Tonga Institute of Higher Education
2
Introduction The main method contains the code that is started when the application is first executed. Until now, we have always used objects that use the main method. But, many objects do not need the main method. Any class that does not need to be started when the application is first executed does not need a main method.
3
Custom Classes and Objects
Sometimes, the objects we have do not fit our needs. We can define custom objects. We can use custom objects just like normal objects
4
Drivers Any class that does not have the main method cannot start by itself Often, developers make tools so they can test their class. A driver is a tool used to begin a program They are useful when testing objects
5
Demonstration StudentDriver
6
Create Our First Class Our class will be a student Each student has a:
School First Name Last Name Program Age Gender
7
Demonstration Student
8
How to Define a Class Special Access Specifier Keyword Class Name
public class Customer { //Body of Class } Access Specifier Class Name Special Keyword
9
Access Specifiers Default – This can be used by:
public class Customer { //Body of Class } Access Specifier Class Name Special Keyword Default – This can be used by: Code in classes in the same package Public – This can be used by everything: Code in classes in different packages
10
Special Keyword and Class Name
public class Customer { //Body of Class } Access Specifier Class Name Special Keyword Keyword Must always be class Class Names Class names should be nouns One word The first letter is capitalized. The first letter of each internal word is capitalized. Keep your class names simple and descriptive. Example: Customer SalesOrder
11
Constructors Constructor – A method that is automatically executed when an object is created. This allows you to set initial values for the object. Many objects have multiple constructors. (They are overloaded) You can find a list of constructors in the Java Developer’s Kit documentation. Show Java Doc
12
Default Constructors All classes have a default constructor
The default constructor is a constructor that accepts no parameters Automatically called when the object is created Default constructors go away when you define your own constructors
13
Creating Constructors
Constructors are methods that have the same name as the class Constructors do not return anything but do not include the word void. To overload constructors Define multiple methods with the same name as the class with different parameters Same name as class Overloaded
14
FavoriteFoods and FavoriteFoodsDriver
Demonstration FavoriteFoods and FavoriteFoodsDriver
15
Garbage Collection When we create objects, we use memory space.
If we never destroy objects, our computer will run out of memory and crash. Fortunately, the Java Garbage collector automatically destroys data. It runs behind the scenes and destroys an object when it detects an object that is no longer used by the program.
16
Finalizers Finalizer – A method that is automatically executed when an object is destroyed. public void finalize() { //Body of method }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.