English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
static methods or blocks belong to the class and they are loaded into memory along with the class. You can call static methods without creating an object. (Use the class name as a reference).
In Java, the " super "this" keyword as a reference to the superclass object. This means that the "super" method should be called by an object, and not by a static method.
Therefore, we cannot use the " super "this" keyword.
In the following Java program, the class ThisExample contains a class with setter and getter methods and instance methodsdisplay()private variableNameWe try to use the this keyword to call the display() method from the main method (a static method).
class SuperClass{ protected String name; } public class SubClass extends SuperClass { private String name; public static void setName(String name) { super.name = name; } public void display() { System.out.println("name: "+super.name); } public static void main(String args[]) { new SubClass().display(); } }
SubClass.java:7: error: non-static variable super cannot be referenced from a static context super.name = name; ^ 1 error