Home
Username:
Password:
Java 6 Tutorials

Introduction / Static

Subtitles of the Movie

One of the first things that you run into when you start programming in Java is the keyword static, but it's not always as well understood as it needs to be. Once you understand how static fits in the overall Java scheme of things, you'll find that it can be a very useful tool. But to understand static, you need to understand how some other things work. As a Java program runs, it constructs objects to get things done. To construct an object, it needs a blueprint for the object that it's going to construct. Such a blueprint is a compiled class. So the first thing that happens is that a class definition is loaded into memory. Loading the class definition is a complete step, but normally precedes the immediate construction of an object from that class definition. Now the object is ready. It's constructor has been called and the object is ready to do work. When the object is constructed, any variables that were defined for it were included in the construction and they now exist. If your program decides to create more objects, it will do so using the same class definition it loaded before and it will create new variables for each one. One class, many objects, with variables for each object. Now, that's the normal way of doing things. But with a static variable, it's slightly different. The variable is fully defined in the class and ready to go as soon as the class is loaded. No object is necessary. The variable is located in the class and ready to go. Adding or deleting objects has no effect on the variable. It is completely defined in the class. There is never more than one copy of it in memory. All this just because the keyword static was used when it was declared. Methods are a little different from variables. All executable code remains with the loaded class definition but normal methods require that an object exist before they can run. When a regular method runs, it contains a reference named this, which holds the address of the object. It is through the this variable that the method has access to all the variables of the object. That is whenever your code refers to a variable, it is necessary to specify which object holds the variable. If you don't specify the object in the method, the default is to use the this reference that was supplied to the method at the time it was called. On the other hand, if the method is static, it can only refer to the class. So an object specification is not necessary. Just remember, regular methods have a reference named this and it uses that to resolve names. Static methods don't have a reference named this. The result is that you can call a static method by referring only to the class name. It doesn't need an object.

Tutorial Information

Course: Java 6
Author: Arthur Griffith
SKU: 33858
ISBN: 1-934743-59-3
Release Date: 2008-02-29
Duration: 7 hrs / 92 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available