Home >Java >javaTutorial >How Do I Define Global Variables in Java?

How Do I Define Global Variables in Java?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-05 08:46:10287browse

How Do I Define Global Variables in Java?

Defining Global Variables in Java

In Java, global variables are declared outside of any method or block. They are accessible from anywhere within the program. To define a global variable, you need to use the static keyword.

Here's an example:

public class Example {
    public static int a;
    public static int b;
}

In the above example, a and b are global variables. You can access them from anywhere within the program by using the class name, followed by the variable name:

Example.a;

Example.b;

The above is the detailed content of How Do I Define Global Variables 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