search
HomeJavaJavaBaseHow are functions (methods) called in java

How are functions (methods) called in java

Nov 22, 2019 pm 01:54 PM
javafunctiontransfer

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

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

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

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!

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
What are different garbage collection algorithms in Java (Serial, Parallel, CMS, G1, ZGC)?What are different garbage collection algorithms in Java (Serial, Parallel, CMS, G1, ZGC)?Mar 14, 2025 pm 05:06 PM

The article discusses various Java garbage collection algorithms (Serial, Parallel, CMS, G1, ZGC), their performance impacts, and suitability for applications with large heaps.

What is the Java Virtual Machine (JVM) and how does it work internally?What is the Java Virtual Machine (JVM) and how does it work internally?Mar 14, 2025 pm 05:05 PM

The article discusses the Java Virtual Machine (JVM), detailing its role in running Java programs across different platforms. It explains the JVM's internal processes, key components, memory management, garbage collection, and performance optimizatio

How do I use Java's Nashorn engine for scripting with JavaScript?How do I use Java's Nashorn engine for scripting with JavaScript?Mar 14, 2025 pm 05:00 PM

Java's Nashorn engine enables JavaScript scripting within Java apps. Key steps include setting up Nashorn, managing scripts, and optimizing performance. Main issues involve security, memory management, and future compatibility due to Nashorn's deprec

How do I use Java's try-with-resources statement for automatic resource management?How do I use Java's try-with-resources statement for automatic resource management?Mar 14, 2025 pm 04:59 PM

Java's try-with-resources simplifies resource management by automatically closing resources like file streams or database connections, improving code readability and maintainability.

How do I use Java's enums to represent fixed sets of values?How do I use Java's enums to represent fixed sets of values?Mar 14, 2025 pm 04:57 PM

Java enums represent fixed sets of values, offering type safety, readability, and additional functionality through custom methods and constructors. They enhance code organization and can be used in switch statements for efficient value handling.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft