static hates non-static

In basic programming we have tried many times (by mistake) to access the non static method of class directly from the static method specifically from the main method and by doing that we get stuck to the following compile time error.

non-static method cannot be referenced from a Static context

Lets consider following example,

public class TestStatic {
    private String name;

    public static void printName() {
       System.out.println(name);//Compile time error

    public static void main(String[] args) {

and above code will give you compile time error, error: non-static variable name cannot be referenced from a static context
 1 error

To understand this we have to consider the difference between static and non-static (i.e instance). So  let’s consider following real life example,

When you were in School as a student you had some specific attributes for the school like rollNumber, standard, name, surname, date of birth, address, contact number etc. So here Student is a class while you are as a student is and Object of the class Student. So Student class is a blue print of all the instances of the Student. Every Student will have details specific to him, so all students of the School will have different details which will not be class specific and belongs to every individual instances of the class so that all the above detail attributes are non-static.

Apart from that Student may have property like schoolName which will be the same for all the Students of your school and will not be Student specific and this property has to be static.So I think you can now differentiate between static and non-static.

static refers to the class and non-static refers to the instance of the class

Non-static fields will be created every time during the creation of instance.While this does not happen with static fields they will get created for the class and will be loaded only once irrespective of the number of instances. So, to access the non-static variable we need instance while static method is accessible with class.

So when you try to refer the non-static field to the static context, which will have same behavior for all the instances, but the non-static variable you are trying to refer will be created when you create an instance of the class. Now this is definitely explain the error message that you are referring to something which is not yet created. 

initialization of static member took place during the initialization of class.

But note here you can definitely refer static in to the non-static context and now you know why.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s