Home > Java > JavaBase > body text

The difference between static and dynamic Java

Guanhui
Release: 2020-06-02 17:00:27
Original
5558 people have browsed it

The difference between static and dynamic Java

The difference between static and dynamic Java

1. Static attributes are shared by classes, while dynamic attributes are independent of each object of the class. own.

2. The static memory space is fixed, and the dynamic memory space is allocated in each subsidiary class.

3. The allocation order is different. Space for static objects is allocated first, and then non-static objects are allocated.

What are the benefits of Java static objects?

1. The data of static objects is globally unique and will be changed once it is changed. If you want to deal with something that is unique in the entire program, making it static is a good way to do it. If you modify a non-static thing, it will only modify its own data, but it will not affect the data of other similar objects.

2. Easy to quote. You can directly use class name.static method name or class name.static variable name to refer to it and modify its attribute value directly without using the get and set methods.

3. Maintain the uniqueness of data. This data is globally unique, and any modification to it will be reflected in all places used in the program. Effectively reduce excess waste.

4. Static final is used to modify member variables and member methods, and can be simply understood as "global constants". For variables, it means that once the value is given, it cannot be modified; for methods, it means that it cannot be overridden.

Code Example

class StaticDemo{
	static int a = 42;
	static int b = 99;
	static void callme(){
		System.out.println("a = "+a);
	}
}
public class TestNew {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		StaticDemo.callme();
		System.out.println("b = "+StaticDemo.b);
	}
}
Copy after login

Recommended tutorial: "Java Tutorial"

The above is the detailed content of The difference between static and dynamic 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