Home > Java > javaTutorial > What are classes and methods in java

What are classes and methods in java

下次还敢
Release: 2024-04-25 22:09:23
Original
1278 people have browsed it

Classes and methods in Java are the basic concepts for creating programs: 1) Classes are templates that define object data and behavior. 2) A method is a block of code that performs a specific operation or calculation. 3) Methods belong to classes, and objects use data in the class by calling methods.

What are classes and methods in java

Classes and Methods in Java

Classes and methods are two basic concepts in the Java programming language. A class is a template that defines the data and behavior of an object. Methods are blocks of code that perform specific operations or calculations.

Class

  • A class is a blueprint that defines the structure and behavior of an object.
  • Classes contain fields (data members) and methods.
  • Fields store the properties of objects.
  • Methods define the behavior of an object.
  • Classes can be used as templates for creating objects.

Methods

  • Methods are blocks of code defined in a class.
  • Methods perform specific operations or calculations.
  • Method can get input parameters and return results.
  • Methods can be used to modify the state of an object or perform operations.

The relationship between classes and methods

  • Methods belong to classes.
  • Objects are instances of classes.
  • Methods can be called through objects.
  • Methods use the object's fields to store and manipulate data.

Example

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

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }
}

public class Main {
    public static void main(String[] args) {
        Student student = new Student();
        student.setName("John");
        student.setAge(20);

        System.out.println("Name: " + student.getName());
        System.out.println("Age: " + student.getAge());
    }
}</code>
Copy after login

In this example, the Student class defines a field named name and a field named age. It also defines four methods: setName, getName, setAge, and getAge. The

Main class creates a Student object student. It then sets the object's field values ​​using the setName and setAge methods. Finally, it obtains the object's field values ​​using the getName and getAge methods.

The above is the detailed content of What are classes and methods in java. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template