Java. Using the keyword ‘static’. Static data members of the class. Static methods of the class




Using the keyword ‘static’. Static data members of the class. Static methods of the class


Contents


Search other websites:

1. For what in classes, static data members are used? Features of the use of static data members

Static members of a class are special members of the class data. They are close to global variables. Static data members are used in classes in cases where it is necessary to use some data regardless of any declared object of this class.

For defined class, you can declare any number of objects of this class. A static member of a class is a common (one) member of the data for all objects in that class. A static member of a class is available, regardless of whether the object of this class was instantiated or not.

If a class member is declared as static in the class, it is available before any object of this class is created and is also accessible without reference to any object.

In fact, the class declaration is the only one in the entire program. Objects (instances) of the class can be several. A static member of a class, like a class declaration, is only one, common to all objects.

2. How do I declare a static member of data? The ‘static’ keyword

A static data member is declared using the ‘static’ keyword. In general, the declaration of a static member of the data in the class looks like this:

class StaticClass
{

    ...

    static type data_member;

    ...

}

3. An example that demonstrates the general access to a static member from different objects in the class and the use of a static method in the class

In the example, a class called DemoStatic is declared. The class declares two instance variables of the class:

  • a static variable of the class with the name sd;
  • an internal variable of class with the name d.

Also in the class are declared methods:

  • class constructor that initializes the value of the internal class variables;
  • methods for accessing the static data member GetSd(), SetSd();
  • methods for accessing the non-static data member GetD(), SetD();
  • static method DemoMethod(), demonstrating the use of static data member sd;
  • the static main() method, which is the entry point to the program.
// Using a class that contains a static data member
public class DemoStatic
{
    static int sd; // A static data member is common to all objects of the class
    int d; // nonstatic data member

    // constructor
    DemoStatic()
    {
        sd = 0;
        d = 0;
    }

    // access methods
    int GetSd()
    {
        return sd;
    }

    int GetD()
    {
        return d;
    }

    void SetSd(int nsd)
    {
        sd = nsd;
    }

    void SedD(int nd)
    {
        d = nd;
    }

    // A static method that demonstrates the use of a static data member
    static void DemoMethod()
    {
        // declaring class objects
        DemoStatic d1 = new DemoStatic();
        DemoStatic d2 = new DemoStatic();
        DemoStatic d3 = new DemoStatic();
        int t; // additional variable

        t = d1.GetD(); // t = 0
        t = d1.GetSd(); // t = 0

        // changing the static data member
        d1.SetSd(5);
        t = d2.GetSd(); // t = 5 - a static data member is common to objects d1, d2, d3
        t = d3.GetSd(); // t = 5 - the same way

        // change of non-static data member
        d1.SedD(8);
        t = d2.GetD(); // t = 0

        System.out.println(t);
    }

    public static void main(String[] args)
    {
        DemoMethod();
    }
}

4. What is the general form of declaring a static method in a class?

As well as the static member of the class data, the static method in the class is declared using the ‘static’ keyword.

In the simplest case, declaring a class with a static method has the following general form:

class StaticClass
{
    // ...

    static type StaticMethod(parameters)
    {
        // ...
    }

    // ...
}

where

  • StaticClass – the name of the class in which the static method is declared;
  • StaticMethod() – static method.

5. What restrictions are imposed on static methods?

Static methods that are declared in a certain class are subject to the following requirements:

  • static methods can only call other static methods;
  • only static variables have access to static methods. Static methods do not have access to static class variables;
  • static methods can not reference to ‘this’ reference of the class;
  • static methods can not use references of type ‘super’.



6. Is it possible to use a class object in a non-static class method if a static member of the data is declared in the class of this object?

No. If the class declares a static data member, then you can not declare an object of this class in the non-static method of the class. In this case, the compiler will generate an error

Cannot make a static reference to the non-static method DemoMethod() from the type DemoStatic

where

  • DemoMethod() – non-static method;
  • DemoStatic – static class.

Such a compiler response is normal, because the non-static method of the class and the static data member belong to different memory classes. You can only use a class object with a static member of data in a static method.

7. Can static variables be declared as hidden with the keyword private?

Yes they can. In the example in step 3, the static member of the class can be declared as follows:

private static int sd; // a static data member declared as 'private'

 

8. What is a static block of the class? Why use a static block of the class?

The static block of class has the following general form:

static
{
    // body of the block
    // ...
}

A static class block is used to initialize static instance variables of a class.

 






9. An example of a class that initializes a static variable in different ways

Method 1. Using the assignment operator ‘=’.

// initializing a static member of the class - method 1
public class InitStatic
{
    static double sd=2.85; // initialization of static member

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        InitStatic s1 = new InitStatic(); // the constructor is called
        double x;

        x = s1.sd; // x = 2.85
        x = sd; // x = 2.85

        System.out.println(x);
    }
}

Method 2. Using the constructor.

// initializing a static member of the class - method 2
public class InitStatic
{
    static double sd;

    // initialization using the constructor
    InitStatic()
    {
        sd = -8.893;
    }

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        InitStatic s1 = new InitStatic(); // the constructor is called
        double x;

        x = s1.sd; // x = -8.893
        x = sd; // x = -8.893

        System.out.println(x);
    }
}

 Method 3. Initialization using a static block.

// initializing a static member of the class - method 3
public class InitStatic
{
    static double sd; // static member initialization

    // Initialization via static block
    static
    {
        sd = -100.0;
    }

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        InitStatic s1 = new InitStatic(); // the constructor is called
        double x;

        x = s1.sd; // x = -100.0
        x = sd; // x = -100.0

        System.out.println(x);
    }
}

 

10. How to access a static method (variable) outside of its class? How to call a static method (variable) using the name of the class in which it is implemented? An example

To call a static method outside the class in which it is declared, the following form is used:

ClassName.StaticMethod()

where

  • ClassName – the name of the class in which the static method is declared.
  • StaticMethod() – the name of the static method.

To call a static variable outside the class in which it is declared, the exact same form is used:

ClassName.StaticVariable

where

  • ClassName – the name of the class in which the static variable is declared;
  • StaticVariable – the name of a static variable.

Example. In the following example, two classes with names A and B are declared. In class A, a static variable and a static method are implemented. A call to a static variable and a class B method from class A is demonstrated.

Implementation of class B:

public class B
{
    static int b; // static variable

    // static variable initialization block b
    static
    {
        b = 13;
    }

    // static method
    static int GetB()
    {
        return b;
    }
}

Implementation of class A:

public class A
{
    // static variable
    static int a;

    // constructor
    A()
    {
        a = 5;
    }

    // static method
    static int GetA()
    {
        return a;
    }

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        // access to a static variable by class name A
        A.a = 8;

        int t;

        // access to the static method of class A
        t = A.GetA(); // t = 8

        // access to a static variable of class B
        t = B.b; // t = 13 - the variable is initialized using a static block of class B
        B.b = 33;
        t = B.b; // t = 33

        // access to the static method of class B
        t = B.GetB(); // t = 33

        System.out.println(t);
    }
}


Related topics