Home > Java > JavaBase > body text

How to call functions (methods) in java

Release: 2019-12-06 16:47:12
Original
28178 people have browsed it

How to call functions (methods) in java

The main method calls in JAVA are as follows: (Recommended: java basic tutorial)

1. Non-static method

Non-static method is a method without static modification. The call of non-static method is called through the object, and the expression is as follows.

对象名.方法()
Copy after login

eg:

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. Calling static methods

Static methods are methods modified with static. Static methods are called through the class name. Expression form As follows:

类名.方法()
Copy after login

eg:

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 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 called in Call it directly in a custom static method.

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 calls a static method in another class, it must be passed through

类名.静态方法();
Copy after login

If it is in a different class, a static method calls a non-static method in another class, it needs to be imported package in this class, and called by creating objects.

(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 need to import the package in the class and call them through the class name to call static methods of other classes.

In different classes, non-static methods When a method calls a non-static method of another class, it needs to import the package in the class and call it by creating an object.

The above is the detailed content of How to call functions (methods) 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