How do you define global variables in Java?

Asked 2 years ago, Updated 2 years ago, 115 views

How do you define global variables in Java?

java global-variable

2022-09-22 13:00

1 Answers

Java does not support global variables grammatically, but you can use the static keyword to use them like global variables.

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

If you create a static variable like this

Example.a = 10;
Example.b = 20;

You can access it this way from where you want to use it.


2022-09-22 13:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.