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

How Do I Define Global Variables in Java?

Mary-Kate Olsen
Release: 2024-12-05 08:46:10
Original
289 people have browsed it

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;
}
Copy after login

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;
Copy after login

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!

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 Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template