search
HomeJavaJavagetting StartedDetailed examples of the concepts of classes and objects in Java

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools