• Nem Talált Eredményt

static CLASS MEMBERS 6.2

In document Programming Languages (Pldal 153-159)

     

If a method or attribute has not been declared with any access modifiers, then the compiler will treat it as public:

int age; since no access modifier is declared, so it will be treated as a public by the compiler

 

static CLASS MEMBERS 6.2

Normally, when you create a class you are describing how objects of that class look and how they will behave. You donÊt actually get anything until you create an object of that class with operator new, and at that point of time data storage are created and methods in the class become available for that object. Each object created from a same class can be in the different states (i.e. their attributes value are not the same; Refer to Figure 5.6 in topic 5). But there are TWO situations in which this approach is not sufficient. These two situations are:

If you want to have only one piece of storage for a particular attribute, regardless of how many objects of this class.

If you need a method that is not associated with any particular object of this class.

That is, you need a method that you can call even if no objects are created.

To address the two situations above we can use the static keyword. Static attribute and methods are not tied to any particular object from a class.

   

Unlike non-static methods and attributes, static attributes exist in only one location and globally accessible by all objects of that class.

 

A static attribute is declared in the same way as an non-static attribute but has the keyword static in front of it. In the following program, the variable count is declared static and thus this variable is the same for all objects of class Student.

Program 6.4:

public class Student {      private String name; 

    private static int count=0; 

    public Student (String NAME) { 

Static attribute or also known as class attribute

Non-static attribute or also known as instance variable

         name=NAME; 

         ++count; 

     } 

     public String getName( ){ 

       return name; 

      } 

      public static int getCount( ){ 

       return count; 

      }  } 

The above program has two attributes ă name (type of String) and count (static int type). This class has one constructor. When this constructor is invoked during object creation, the attribute name will be assigned with the value of parameter in this constructor and at the same time the static attribute count will be added with 1. This class also has two member methods ă getName() method to retrieve the value of the attribute name and getCount() method to retrieve the value of the static attribute count.

To execute the above program, we need to create an object of the class Student in the method main() as shown in the following program:

Program 6.5:

 

Line 1  class obj { 

Line 2    public  static void main (String[ ] args){ 

Line 3        Student s1 = new Student (“Kamil”); 

Line 4         System.out.println(“Name of s1=” + s1.getName()); 

Line 5        System.out.println(“count value =” + s1.getCount()); 

Line 6        Student s2 = new Student (“Ahmad”); 

Line 7         System.out.println(“Name of s2=” + s2.getName()); 

Line 8        System.out.println(“count value =” + s2.getCount()); 

Line 9        s1=null; 

Line 10        s2=null; 

Line 11       System.out.println(ʺcount value =ʺ + Student.getCount()); 

Line 12     }  Line 13  }   

The output for the above Program 6.5 is shown below:

 

Name of s1=Kamil count value =1 Name of s2=Ahmad count value =2 count value =2

The following figures 6.1(a), (b), (c) and (d) visualise of how the above output

Figure 6.1(a): Effects in computer memory when Line 3 is executed Student Class

Figure 6.1(b): Effects in computer memory when Line 6 is executed. Notice that the attribute static attribute count is shared by the objects s1 and s2

 

Figure 6.1(c): Effects in computer memory when Line 9 is executed Student Class

String name

static int count Object Sudent: s1 name= null

Figure 6.1(d): Effects in computer memory when Line 10 is executed

Execution of lines 4, 5, 7 and 8 do not give any effect to the memory. The statements in these lines are merely retrieving the current value of the attributes from the memory and displaying them on the screen.

         

The following statements in Program 6.5 (Line 9 and Line 10) will assign a null value to the s1 and s2 objects. The effect of this instruction is s1 and s2 will not reference to any object.

s1=null;

s2=null;

 

public static void update2( ){

int total=a; //wrong ăa is a non-static attribute update1(); // wrong ăupdate1 is a non-static method }

}

Source: Liang Y.D (2007) static int b=7;

Static attributes and static methods can be used in both non-static methods or static methods. However, non-static methods and attributes can only be used in non-static methods and not in static methods.

class A{

int a=9;

ClassName.StaticAttribute();

Note: Make sure StaticMethod() and StaticAttribute are declared as public if you want to call them from outside of the class in which they have declared (whatever you have learned in section 6.1 also apply for static attributes and methods)

Caution⁄

Why using Student.getCount() in Line 11 instead of s1.getCount() or s2.getCount to retrieve the value of the static attribute count?

In Line 10 and Line 11, the object s1 and s2 have been set to null. It means they do not reference to any objects. If you have never created an object of a class or has set the object to null, you can only call static method or access the static attribute using the class name (and not the object name) as shown in the following syntax:

ClassName.StaticMethod();

OR

Caution⁄

Static attributes exist in only one location and globally accessible by all objects of that class. But they are also subject to class scope if there are given private access modifiers. If a static attribute or method is given a private modifier it can only be accessed in that class. But we still can retrieve the value of a private static attribute from outside of a class using a public method (declared in the same class) that will returned the value of the private static attribute.

USING this REFERENCE

In document Programming Languages (Pldal 153-159)