Home> Java> javaTutorial> body text

A brief discussion on how to define global variables in Java development

无忌哥哥
Release: 2018-07-19 10:12:02
Original
4683 people have browsed it

Static static variables

Any variables or codes in the program are automatically allocated memory for storage by the system during compilation, and the so-called static This means that the memory allocated after compilation will always exist, and the memory space will not be released until the program exits.

static is the static modifier. Members modified by static are allocated a memory space by the system during compilation and will not be released until the program stops running. That means that all objects of this class will share this memory space.

import java.util.*; public class Main{ static int ans;//定义全局变量 public static void main(String[] args){ Scanner scan = new Scanner(System.in); int n; while(scan.hasNext()){ ans = 0; n = scan.nextInt(); System.out.println(fib(n)+"\n递归调用了"+ans+"次"); } } private static int fib(int n){ ans ++; if(n == 1 || n == 2) return 1; return fib(n-1)+fib(n-2); } }
Copy after login

The above is the detailed content of A brief discussion on how to define global variables in Java development. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!