Home  >  Article  >  Java  >  How do Java functions compare? How are they different?

How do Java functions compare? How are they different?

WBOY
WBOYOriginal
2024-04-20 11:24:02339browse

There are two types of Java functions: static methods and instance methods. Static methods do not require object instances, are called directly through the class name, and are used to perform operations unrelated to the class state. Instance methods require an object instance to be called and are used to access or modify the state of the object.

How do Java functions compare? How are they different?

Java Function Comparison

In Java, there are two types of functions: static methods and instance methods. They have different purposes and usages as follows:

Static method (class method)

  • Declared using static keyword
  • No object instance required, called directly by class name
  • Used to perform operations independent of class state, such as mathematical calculations or utility functions

Code example:

public class MathUtils {

    public static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        int result = MathUtils.add(5, 10);
        System.out.println(result); // 输出:15
    }
}

Instance method

  • Not used static Keyword declaration
  • Required Only an object instance can be called
  • to access or modify the state of the object, such as getters and setters

##Code example:

public class Person {

    private String name;

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

    public String getName() {
        return name;
    }

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

    public static void main(String[] args) {
        Person p1 = new Person("John");
        p1.setName("Mary");
        System.out.println(p1.getName()); // 输出:Mary
    }
}

Main differences

FeaturesStatic methodsInstance methodsInstantiationNot requiredRequiredCalling methodClass name.methodName() object.methodName()PurposeOperation independent of the objectAccess or modify the state of the objectVisibilityAccessible from anywhereAccessible only from within the same instance

Practical case

Write a static method to splice any two strings:

public class StringUtils {

    public static String concat(String str1, String str2) {
        return str1 + str2;
    }

    public static void main(String[] args) {
        String name1 = "John";
        String name2 = "Doe";
        String fullName = StringUtils.concat(name1, name2);
        System.out.println(fullName); // 输出:JohnDoe
    }
}

The above is the detailed content of How do Java functions compare? How are they different?. 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