static

15 0 0
                                    

static - It is a class member. A class member is the same member through out the class no matter how many objects you created. A static member should be called using the class name. Sometimes, static modifier is used together with final modifier. final means you don't want to change the value since it is constant.

    Example:

    static final PI = 3.14;

Below is the example of using a static modifier. Usually a  static method is used to accessed a static field or variable. No need to complicate things. When static, use a class name to access it. When instance, use an object to access the fields and methods. 

=========================

class StudentInformation {
    private int id;
    private String firstName;
    private String middleName;
    private String lastName;
    private static int studentCounter = 0;
    

    StudentInformation(String fn, String mn, String ln){
        id = ++studentCounter;
        firstName = fn;
        middleName = mn;
        lastName = ln;
    }


    void displayInfo(){
        System.out.println("id: " + id);
        System.out.println("name: " + firstName + " " + middleName + " " + lastName);
    }


    static int getStudentCounter(){
        return studentCounter;
    }
}

class PracticeMay21 {
    public static void main(String[] args){
        StudentInformation studentInformation = new StudentInformation("Bucky","Downy", "Roberts");
        studentInformation.displayInfo();
        StudentInformation studentInformation1 = new StudentInformation("Bell", "Collins", "Crane");
        studentInformation1.displayInfo();
        System.out.println(StudentInformation.getStudentCounter());
    }
}

=============================

id: 1

name: Bucky Downy Roberts

id: 2

name: Bell Collins Crane

2


Instance variables is initialized in constructors because simple initialization will not suffice if the initialization contains logic such as the used of array and for loop.

static initialization block - when you say block, it the between two braces {}. Since it is static, there should be a word static in front of the block and the initialization should be done within that static block. Remember that in this block, you just initialize the variables and not declare within it.

instance initialization block - it is the same with the static block but you just omit the static keyword. So, what is left is just the block. When you initialized in either the static or instance initialization block, the compiler copies it to every constructor.

initialization using a method - it is common to initialize either static or instance using a method because you can reuse it later on rather those initialization blocks.


Rearranging the above example:

===============================

class StudentInformation {
private int id;
private String firstName;
private String middleName;
private String lastName;
private static int studentCounter;
static{
studentCounter = 0;
}

{
id = ++studentCounter;
}

StudentInformation(String fn, String mn, String ln){
initializationMethod(fn, mn, ln);
}


void displayInfo(){
System.out.println("id: " + id);
System.out.println("name: " + firstName + " " + middleName + " " + lastName);
}


static int getStudentCounter(){
return studentCounter;
}


private void initializationMethod(String f, String m, String l){
firstName = f;
middleName = m;
lastName = l;
}
}

============================

In this example, I specify the use of both initialization block and also a method where I did some initialization. Just don't forget to invoke your method in the constructor.


Rules of methods in accessing class members

1. Instance methods can access instance variables and instance methods directly.

2. Instance methods can access static variables and static methods directly.

3. Static methods cannot access instance variables and instance methods directly. You need to use an object reference but you cannot use the this keyword since there is no instance for this to refer to.

4. Static methods can access static variables and static methods directly.


Java ProgrammingWhere stories live. Discover now