Home  >  Article  >  Java  >  Detailed examples of the concepts of classes and objects in Java

Detailed examples of the concepts of classes and objects in Java

王林
王林Original
2020-05-19 18:08:271855browse

Detailed examples of the concepts of classes and objects in Java

In Java, a class can be regarded as a template for creating Java objects.

The definition of a class in Java:

public class Dog{
string breed;
int age;
string color;

void barking(){
}

void hungry(){
}

void sleeping(){
}
}

A class can contain the following types of variables:

1. Local variables: in methods, constructors or Variables defined within a statement block are called local variables. Variable declaration and initialization are all in methods. After the method ends, the variables will be automatically destroyed.

2. Member variables: Member variables are variables defined in the class and outside the method body. Such variables are instantiated when the object is created. Member variables can be accessed by methods, constructors, and statement blocks of a specific class.

3. Class variables: Class variables are also declared in the class, outside the method body, but they must be declared as static type.

A class can have multiple methods. In the above example: barking(), hungry() and sleeping() are all methods of the Dog class.

(Video tutorial recommendation: java video)

Constructor method

Each class has a constructor method. If no constructor is explicitly defined for a class, the Java compiler will provide a default constructor for the class.

When creating an object, at least one constructor must be called. The name of the constructor must be the same as the class. A class can have multiple constructors.

Constructor method example:

public class puppy{
    public puppy(){
    //无参构造方法
    }
    
    public puppy(String name){
    //有一个参数的构造方法
    }
}

Creating objects

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:

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

2. Instantiation: Use the keyword new to create an object.

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

Example of creating an object:

public class Dog{
    public Dog(String name){
        System.out.println("我的小狗的名字是:" + name);
    }
    
    public static void main(String[] args){
        Dog xd = new Dog("小D");
    }
}

Access instance variables and methods

Access member variables and member methods through the created object, as follows Display:

Instance

Access instance variables and call member methods:

public class Dog{
    int DogAge;
    public Dog(String name){
        System.out.println("这是我的小狗:" + name);
    }
    
    public void setAge(int age){
        DogAge = age;
    }
    
    public int getAge(){
        System.out.println("它的年龄是:" + DogAge);
        return DogAge;
    }
    
    public static void main(String[] args){
        Dog dog = new Dog("小D");
        dog.setAge(4);
        dog.getAge();
    }
}

Recommended tutorial: Getting started with java development

The above is the detailed content of Detailed examples of the concepts of classes and objects in Java. 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