Static Class in Java: Definition & Examples

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: How to Convert String to Int in Java - ParseInt Method

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:00 Static Doesn't Mean Stagnant
  • 0:54 Examples
  • 2:14 Static Classes &…
  • 4:05 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.

Java lets you create classes within classes. These static nested classes have unique features that will be discussed within this lesson. Working code examples are provided.

Static Doesn't Mean Stagnant

In Java, static is a keyword used to describe how objects are managed in memory. It means that the static object belongs specifically to the class, instead of instances of that class. Variables, methods, and nested classes can be static.

Think of a class for a book. How many instances of class 'Book' could there be? If we created a variable for page count and did not make it static, that variable would be re-used every time a new instance of the 'Book' class was created. Instead, we can make the variable static and make it part of the class itself. The same holds true for classes themselves. A static class is really a class within a class. In fact, static classes are often called static nested classes.

Examples

Let's look at some examples that show the nested class in action. First, let's look at how the nested class looks inside the outer class. In this example, the outer class is HotBeverage and the static inner class is HotTea.

public class HotBeverage {
  private static String note = "Hot Beverage";
   public static class HotTea {
    public void displayOutput() {
     System.out.println("Hot Tea says: " + note);
   }
  }
}

Next, we'll move to the main method of the program and create an instance of the static class, as well as invoke the method within the class. In order to create an instance of an inner class, you don't need to create an instance of the outer class. Instead, you first reference the outer class, followed by a period and the name of the inner class. Once that's done, we have access to the methods within the static class.

public static void main(String[] args) {
  //create instance of static class
  HotBeverage.HotTea ht = new HotBeverage.HotTea();
  //call the method we made
  ht.displayOutput();
}

When this code runs, this message is displayed:


Java create instance of static class output


Static Classes & Non-Static Variables

A word of caution at this point. While the static class can access members (variables/methods) in the outer class, it can only access them if they are static themselves. This is why we're able to use the string variable in the nested class. It was defined as a static variable. However, if we create a non-static variable in the outer class, we cannot reference it in the static class. Thus, this code contains an error:

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