Home > Java > javaTutorial > Can a class in java only create one object?

Can a class in java only create one object?

下次还敢
Release: 2024-04-29 02:30:21
Original
891 people have browsed it

No, a class can create multiple objects in Java. An object is an instance of a class that stores data values ​​specific to that class. Each class can create multiple objects, each with its own unique data values.

Can a class in java only create one object?

Can a class in Java only create one object?

no. In Java, a class can create multiple objects.

Objects in Java are instances of classes. A class defines the data types and behavior of an object, and objects store data values ​​specific to that class. When an object of a class is created, the Java Runtime Environment (JRE) allocates memory to store the object's instance variables.

For example, consider the following class:

<code class="java">class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // ...其他方法
}</code>
Copy after login

We can use this class to create multiple objects:

<code class="java">Person person1 = new Person("John", 25);
Person person2 = new Person("Mary", 30);</code>
Copy after login

These objects are all instances of the Person class , but they are different objects with different data values.

So, in Java, a class can create multiple objects, each of which stores its own set of data values.

The above is the detailed content of Can a class in java only create one object?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template