Menu

Cannot make a static reference to the non-static method

Cannot make a static reference to the non-static method display(String) from the type RunAndTest.
We cannot access or call a non static method or variable from a static method or main method(which is also a static method) of a class. Since non-static method and variables belongs to the instance of the class and we call them instance variable and method, but static method and variable are directly belongs to class which we called member variable and method.
We can access a non-static variable and method in static method only using the instance of the class. But if you want to access any non-static variable and method in non-static method of the same class then we can access them without instance of the class.


Why we can not access? 

As soon as our program loaded into memory at the same time all static variable, method and block get loaded in the memory as well, and JVM will maintain only single copy of those static thing through all over the class. And all non-static components will get loaded into the memory when we create instance of that class that why we called them instance variable/method, and when you create another instance of that class then a new and separate memory will be get allocated for all those variables and methods for that instance.

In case if you will write a code to access a non-static member in static method without instance of that class then compiler will through the error "Cannot make a static reference to the non-static method".

Below is a code sample which you could just copy and paste in your IDE and understand the concept of this static and non-static.

Compilation fail while accessing a non-static method in static method.


package coreJava;
public class RunAndTest {
public static void main(String[] args) {
display("string"); //This line of code will through compile time error "cannot make a static reference to the non-static method display(String) from the type RunAndTest"
}
void display(RunAndTest rt){
System.out.println("Test");
display("sdj");
}
void display(String string){
System.out.println("String method");
}
}

Access a non-static method in static method, with the help of object of the class.


package coreJava;public class RunAndTest { public static void main(String[] args) { RunAndTest rt=new RunAndTest(); rt.display("string"); 
} void display(RunAndTest rt){ System.out.println("Test"); display("sdj"); } void display(String string){ System.out.println("String method"); }} 

1 comment:

  1. G club Everybody's chance to join the Football Star Online Slots.

    G club It's one more chance for everyone to join the Football Star Online Slots and other gambling games over the Internet. We are most of the online gambling casinos that have been endorsed by gamblers. With over 12 years of experience in the service industry.
    Football Star Online Slots is a great slots game where you'll be entertained with the images and colors of the football game that we created. Then you will find yourself in the middle of the field to prepare to kick in the football game. Because of the nature of the players. You will choose what you want to be. While rotating the slots, the player will have the option to earn a simple return. But you have to cooperate with each other. Which is considered the most in this game. In addition to the Football Star Online Slots game, we also have the most endangered games, more than 500 roster.
    Wait for all of them to come up with an exotic expertise in gaming, betting on your own internet network. We do not have to wait 24 hours at the site. จีคลับ

    ReplyDelete