Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing static method through an object instance

I have learnt that if we wish to call static method of another class then you have to write class name while calling static method. In below program, I created object of the Employee class inside Employee_Impl class and using that object, I was still able to access the count method. Why is it allowing me to use count method through an object if static methods are accessed using only class name? Does that mean static methods can be accessed using objects as well as class name, both?

Employee.java

public class Employee{
    static int counter = 0;
    static int count(){
        counter++;
        return counter;
    }
}

Employee_Impl.java

class Employee_Impl
    public static void main(String args[]){
        Employee obj = new Employee();
        System.out.println(obj.count());
        System.out.println(Employee.count());
        System.out.println(obj.count());        
    }
}

output

1
2
3
like image 810
Coding Bad Avatar asked Aug 16 '15 20:08

Coding Bad


People also ask

Can I access static method using object?

Static methods can't access instance methods and instance variables directly. They must use reference to object. And static method can't use this keyword as there is no instance for 'this' to refer to.

Can you call a static method from an instance?

Java syntax allows calling static methods from an instance. For example, we could create this code and it would compile and run correctly: public static void main(String args) { Example ex = new Example();

Can you access a static variable from an instance?

Can we access static variables from instance and static methods? Yes, static members (static variables) can be accessed from both instance and static area (i.e. instance and static methods) directly using the class name or without the class name.

Can you use static methods on objects in Java?

Java allows developers to define static methods, which are also available to every instance of a class. In an instance of a class, static methods cannot access variables in an instance and those belonging to a class. They can only access static fields and have to use object reference.


2 Answers

Compiler automatically substitutes this call by call by class name of your variable (not of it's value!). Note, even if your object would be null, it will work, no NullPointerException is thrown.

like image 158
Philip Voronov Avatar answered Oct 04 '22 15:10

Philip Voronov


You are allowed to do this because you made an instance of Employee to access the method through.

The point of static methods is to allow access to "utility methods" that can be invoked without the overhead of instantiating a new object. Furthermore, these methods can be shared by any instance of the class Employee and can mutate shared static variables or attributes. For example, pretend the shared prefix of all Employee objects was maintained as a static attribute:

public class Employee {
    private static String EMPLOYEE_IDENTIFIER_PREFIX = "Acme Corporation Employee Number:"

    public static void setEmployeeIdentifierPrefix(String prefix){
         Employee.EMPLOYEE_IDENTIFIER_PREFIX = prefix;
    }
}

If AcmeCorporation was purchased by MultinationalCorporation the prefix can be updated for all Employee objects by using the setEmployeeIdentifierPrefixMethod on the class like so:

Employee.setEmployeeIdentifierPrefix("Multinational Corporation Employee Number:");
like image 35
andrewdleach Avatar answered Oct 04 '22 15:10

andrewdleach