Home  >  Article  >  Java  >  What is a java object?

What is a java object?

(*-*)浩
(*-*)浩Original
2019-05-21 15:53:4930887browse

There is a popular saying in Java, called "Everything is an object". This is one of the concepts at the beginning of Java language design. To understand what an object is, it needs to be understood together with a class.

What is a java object?

Object:

Object is anything that people want to study. It can not only represent specific things, but also Abstract rules, plans, or events. Objects have state, and an object uses data values ​​to describe its state. Objects also have operations, which are used to change the state of the object. The object and its operations are the behavior of the object. Objects realize the combination of data and operations, so that data and operations are encapsulated in the unity of objects.

Class:

The abstraction of objects with the same characteristics (data elements) and behavior (function) is a class. Therefore, the abstraction of an object is a class, and the concretization of a class is an object. It can also be said that an instance of a class is an object, and a class is actually a data type. Classes have attributes, which are abstractions of the state of objects, and use data structures to describe the attributes of the class. A class has an operation, which is an abstraction of the object's behavior, described by the operation name and the method to implement the operation.

The relationship between objects and classes:

Objects are created based on classes. In Java, use the keyword new to create a new object. Creating an object requires the following three steps:

Declaration: Declare an object, including the object name and object type.

Instantiation: Use the keyword new to create an object.

Initialization: When using new to create an object, the constructor method will be called to initialize the object.

public class Puppy{
   public Puppy(String name){
      //这个构造器仅有一个参数:name
      System.out.println("小狗的名字是 : " + name ); 
   }
   public static void main(String[] args){
      // 下面的语句将创建一个Puppy对象
      Puppy myPuppy = new Puppy( "tommy" );   
   }
}

Related learning recommendations: java basic tutorial

The above is the detailed content of What is a java object?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn