Java Global Variable: Declaration & Examples

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Java If Statement with Integers

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Global Variables
  • 0:46 Static Variables
  • 1:55 Constant Variables
  • 2:46 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Unlike C/C++, there is not a true global variable in Java. However, we can get close. This lesson will show you how to declare semi-global variables in Java using working code examples.

Global Variables

If you have experience with other programming languages, such as C and C++, you have probably used global variables. A global variable is one that is accessible to all parts of a program and is usually declared as part of the first lines of code.

Java doesn't technically support global variables. As a pure object-oriented language, everything needs to be part of a class. The reason is to protect data and members of classes from being changed, inadvertently or on purpose, by other parts of the program. However, you still may need to have variables that can be accessed across your program, and not confined to specific classes.

Let's take a look at some ways to achieve this, starting with static variables.

Static Variables

Because Java is object-oriented, all variables are members of classes. However, we can create static variables that are more accessible. The static modifier tells Java that the variable can be used across all instances of the class. Therefore, if you create a variable for pay rate and make it static, any instance of the parent class can access pay rate.

This code shows how we declare two variables within a class. Although we're declaring price and pages in the Openclass class, they are static variables and can now be used by instances of Openclass.

public class Openclass {
  public static double price = 15.24;
  public static long pages = 1053;
}

In another class, we can get to those variables by simply denoting the class, putting in a period, and finally the field name. The following code creates new variables but assigns the value of the global variables to these new variables:

public static void main(String[] args) {
  double newPrice = Openclass.price;
  long newPages = Openclass.pages;
  System.out.println(newPrice);
  System.out.println(newPages);
}

When this program runs, we get this output:


Java global methods output


You can now see that the traditional global variable doesn't exist in Java. Yes, we technically have variables available across the program, but we also created additional variables to hold the values of these static variables.

However, there is another way to create a somewhat global variable. We'll discuss that next.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support