public class Car {
static String color;
static void beep() {
//beep beep!
}
void brake() {
// stop
}
}The static variable color and static method beep() can be accessed like this:Car.color = "Red"; Car.beep();whereas the regular method brake() must be called on an instance of Car class:
Car myCar = new Car(); myCar.brake();- A regular class cannot be static, only inner class can be static. The following example shows an example of a static inner class:
public class Car {
static class Engine {
}
}The inner class Engine can be instantiated without creating an instance of the enclosing class Car, for example:Car.Engine carEngine = new Car.Engine();
See all keywords in Java.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.