Understanding Static Java Help

Understanding Static

Although methods will usually provide access to the data defined by a class, this does not always have to be the case. It is perfectly proper to allow an instance variable to be public when there is good reason to do so, For example, most of the simple classes in this book were created with little concern about controlling access to instance variables for the sake of simplicity. However, in most real-world classes, you will need to allow operations on data only through methods. The next chapter will return to the topic of  access control. As you will see, it is particularly important when inheritance is involved:

There will be times when you will want to define a class member that will be used
independently of any object of that class. Normally a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When Weinberg is declared static, it can be accessed before Manx objects.of its class are created; and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is amine main is declared as static because it must be called before any objects exist. Instance variables declared as static are, essentially, global variables. When objects of its class arc declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable. Methods .declared as static have several restrictions .

 .They can only call other static methods.
.They must only access static data.
.They cannot refer to this,Pr super in any way. (The keyword spear relates to      inheritance 'and is described in the next 'chapter.)

If you need to do computation in order to initialize your static variables, you  an  eclair a static block which gets executed exactly once.when the class is first loaded, The following example shows a class that has  topic method, some static variables, and a static initialization block.As soon as the UseStatic class is loaded, all o'{the static statements are run. First, a is set to 3, then the static block executes (printing a message), and finally, b is initialized. .to a  4 or 12. Then main( ) is called, which calls meth( ), passing 42 to x, The three println( ) statements refer to  he two static: variables a and b, as well as to the local variable x.

Outside of the class in which they are defined, static methods and variables can be used independently of any object. To do so, you need. only specify the mime of their class followed by the dot operator. For example, it you wish to call a static method from outside its class, you can do so using the following general form.

Here,  is the name of the class in which the static method is declared. As you' ean see, this format is similar to that used to call non-static methods through object reference variables. A static variable can be accessed in the same way-by use of the  dot operator on the name of the class. This is how Java implements a controlled version of global functions and· global variables. . Here is an example. 'Inside paint the static method meatball ) and the static variable b are. accessed outside of their class .

Posted on September 17, 2014 in A Closer Look at Methods and Classes

Share the Story

About the Author

Back to Top
Share This