search
HomeJavaJavaBaseHow to call functions (methods) in java

How to call functions (methods) in java

Nov 26, 2019 am 10:10 AM
java

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.

对象名.方法()

eg:

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

2. Calling static methods

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

类名.方法()

eg:

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

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");
    }}

If a static method calls a static method in another class, it must be passed through

类名.静态方法();

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!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools