Home > Java > JavaBase > How are functions (methods) called in java

How are functions (methods) called in java

王林
Release: 2019-11-22 13:54:26
Original
4616 people have browsed it

How are functions (methods) called in java

There are mainly the following types of method calls in JAVA:

1. Non-static methods

Non-static methods are methods without static modification. For calls to non-static methods, they are called through objects. The expression is as follows:

Object name.Method()

public class InvokeMethod{
    public static void main(String[] args){
        InvokeMethod in = new InvokeMethod();
        in.t1();
    }
    public void t1(){
        System.out.printfln("t1");
    }}
Copy after login

2. Call static Method

Static method is a method modified with static. The static method is called through the class name. The expression is as follows:

Class name.Method()

public class InvokeMethod{
    public static void main (String[] args){
        InvokeMethod.t2();
    }
    public static void t2(){
        System.out.println("static t2....");
    }}
Copy after login

3. Calls between methods

Calls between methods are mainly how to call other methods within a method.

(1) Call other methods inside the static method

If in this class, the static method can directly call the static method, in addition to the main method, it can also be in the custom static method Direct call, if it is a non-static method in this class, it must be called through the object.

public class InvokeMethod{
    public static void main (String[] args){
        t2();
    }
    public static void t2(){
        System.out.println("static t2...");
    }
    public static void t1(){
        //静态方法调用非静态方法需通过对象来调用
        //InvokeMethod in =new InvokeMethod();
        //in.t2();
        t2();
        System.out.println("static t1");
    }}
Copy after login

If a static method is not in a class, calling a static method in another class must pass

class name.static method();

If a static method calls a non-static method of another class in different classes, you need to import the package in the class and call it by creating an object.

(2) Internal calls of non-static methods

If in this class, non-static methods can directly call static methods and non-static methods; in different classes, non-static methods call other classes The static method needs to be imported by the package in the class, and needs to be called by the class name; in a different class, when the non-static method calls the non-static method of other classes, the package in the class needs to be imported, and it needs to be created by object to call.

Recommended tutorial: Getting started with java development

The above is the detailed content of How are functions (methods) called in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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