Objects in Java - and any other "object-oriented" language - are the basic building blocks of all Java applications, representing any real-world object you might find around you: an apple, a cat, a car, or a human.
The two characteristics that an object always has are state and behavior. Consider a character. Its status may include hair color, gender, height and weight, as well as feelings of anger, frustration, or love. Its actions may include walking, sleeping, cooking, working, or anything else a person might do.
Objects form the core of any object-oriented programming language.
What is object-oriented programming?
Hundreds of books have been written describing the intricacies of object-oriented programming, but basically, OOP simplifies development time based on a holistic approach that emphasizes reuse and inheritance. More traditional procedural languages, such as Fortran, COBOL, and C, take a top-down approach, breaking down a task or problem into a logically ordered series of functions.
For example, consider a simple ATM application used by a bank. Before writing any code, a Java developer first creates a roadmap or plan of how to proceed, usually starting with a list of all the objects that need to be created and how they will interact. Developers can use class diagrams to clarify the relationships between objects. The objects used in ATM transactions may be currencies, cards, balances, receipts, withdrawals, deposits, etc. These objects need to work together to complete the transaction: for example, a deposit should produce a balance report and perhaps a receipt. Objects will pass messages between them to complete tasks.
Objects and Classes
An object is an instance of a class: this is a key and reusable concept in object-oriented programming. Before an object can exist, there must be a class on which it can be based.
Maybe we want a book object: to be precise, we want a book called "The Hitchhiker's Guide to the Galaxy." We first need to create a class Book. This lesson could be the basis for any book in the world.
It might look like this:
public class Book { String title; String author; //methods public String getTitle( { return title; } public void setTitle() { return title; } public int getAuthor() { return author; } public int setAuthor() { return author; } // etc. }
The class Book has a title and an author, and its methods allow you to set or get any of these items (it also has more elements, But this example is just an excerpt). But it's not an object yet - Java applications can't do anything with it yet. It needs to be instantiated to become a usable object.
Create an object
The relationship between objects and classes is this: a class can create multiple objects. Each object has its own data, but its underlying structure (for example, the type of data it stores and its behavior) is defined by the class.
We can create several objects from the book class. Every object is called an instance of a class.
Book HitchHiker = new Book("The HitchHiker's Guide to the Galaxy", "Douglas Adams"); Book ShortHistory = new Book("A Short History of Nearly Everything", "Bill Bryson"); Book IceStation = new Book("Ice Station Zebra", "Alistair MacLean");
These three objects are now available: they can be read, purchased, borrowed or shared.
The above is the detailed content of How to create Java objects. For more information, please follow other related articles on the PHP Chinese website!