search
HomeJavaJavaBaseWhat is the difference between static methods and non-static methods in java

Difference: 1. Static methods are methods modified with the static keyword and belong to classes, not objects; non-static methods are ordinary methods that are not modified with the static keyword, belonging to objects but not classes. 2. Static methods can be called directly, class name calls and object calls; non-static methods can only be called through objects. 3. Different life cycles.

What is the difference between static methods and non-static methods in java

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

1. The difference between static methods and non-static methods (calling objects and reference variables are different)

Static method: It is a method modified with the static keyword, also called Class method. It belongs to the class, not to the object. You can call the static method through the class name and method name before instantiating the object. (Static attributes and static methods belong to classes and can be called directly through the class name).

A. In a static method, you can call a static method.

B. In a static method, non-static methods cannot be called.

C. In a static method, you can reference class variables (that is, variables modified by static).

D. In a static method, member variables cannot be referenced (that is, variables without static modification).

E. In static methods, the super and this keywords cannot be used

Non-static method: It is an ordinary method that does not contain the static keyword modification, also known as instance method and member method. Belongs to objects, not to classes. (Member attributes and member methods belong to the object, and the object must be created through the new keyword and then called through the object).

A. In ordinary methods, ordinary methods can be called.

B. In ordinary methods, you can call static methods

C. In ordinary methods, you can reference class variables and member variables

D. In ordinary methods, You can use the super and this keywords

2. The difference between static methods and non-static methods (different calling methods)

Static methods can be called directly, and the class name call and Object call. (Class name. Method name / Object name. Method name)

But non-static methods can only be called through objects. (Object name. Method name)

3. The difference between static methods and non-static methods (different life cycles)

The life cycle of a static method is the same as that of the corresponding class Long, static methods and static variables are allocated and loaded into memory as the class is defined. Static properties and methods will not be destroyed until the thread ends. (That is, static methods belong to classes)

The life cycle of non-static methods is as long as the instantiated object of the class. Only when the class instantiates an object, the non-static method will be created, and when this object When destroyed, non-static methods are also destroyed immediately. (That is, non-static methods belong to objects)

Summary: Class methods can be called directly through the class name, instance methods must first instantiate the class, then initialize the object, and then call it through the instance object of the class

Example:

class XYZ{
public static void main(String[] str){
     XYZ.testStatic();         //直接通过类调用

     XYZ a = new XYZ();        //实例化,然后构造方法会初始化
     a.testMethod();           //对象调用方法
}

public static void testStatic(){
System.out.println("This is static method");
}

public void testMethod(){
System.out.println("This is instance method");
}
}

Static static variables/methods are initialized during the class loading process, and there is only one copy in the memory, so it can be regarded as a global variable/method.

Recommended related video tutorials: Java video tutorial

The above is the detailed content of What is the difference between static methods and non-static 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
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java数据结构之AVL树详解Java数据结构之AVL树详解Jun 01, 2022 am 11:39 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor