English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Can we have a static reference to a non-static field in Java?

In Java, a class will have three types of variables, namely static (class), instance, and local variables.

  • Local variables-These variables belong to the method/Block/Constructor and declare/Definition. The scope of these variables is within the method (or block or constructor), and they will be destroyed after execution.

  • Instance variables-These variables belong to the class instance (object). They are declared within the class but outside the method. They are initialized when the class is instantiated. They can be accessed from any method, constructor, or block of the specific class.

    You must use an object to access instance variables. That is, to access instance variables, you need to create an object of the class and use the object to access these variables.

  • Class/Static variables-Class/Static variables belong to the class, just like instance variables; they are declared outside any method within the class but are declared using the static keyword.

    They can be accessed at compile time, and you can access them before/When accessing without instantiating the class, there is only one copy of the static field available throughout the class, that is, the value of the static field is the same in all objects. You can use the static keyword to define a static field.

Static reference to non-static variable e

As shown above, use class name reference (access) to static variables.

System.out.println(MyClass.data);

That is to say, using a static reference to reference a variable means using the class name reference.

However, to access instance variables, you must create an object; before instantiation, these objects are not available in memory.

Therefore, you cannot statically reference non-static fields (variables) in Java. If so, try to do this, thus generating a compile-time error indicating that 'Cannot reference non-static variable 'math' from a static context'.

Example

Following the Java program, accept user marks and determine whether he has been promoted.

Here, from the static method wasPromoted(), we directly access instance variables (just by specifying their names, as if they were static variables). Since this is not allowed, a compile-time error will occur.

import java.util.Scanner;
public class StudentMarks {
   Scanner scan1 = new Scanner(System.in);
   private double math;
   private double science;
   private double english;
   public StudentMarks(double math, double science, double english) {
      this.math = math;
      this.science = science;
      this.english = english;
   }
   public static boolean wasPromoted(StudentMarks marks) {
      if(math >=85 && science >=75 && english >=65) {
         return true;
      }
      return false;
   }
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your math score:");
      double math = sc.nextDouble();
      System.out.println("Enter your science score:");
      double science = sc.nextDouble();
      System.out.println("Enter your english score:");
      double english = sc.nextDouble();
      StudentMarks marks = new StudentMarks(math, science, english);
      boolean bool = wasPromoted(marks);
      if(bool) {
         System.out.println("Congratulations, you've got promoted");
      } else {
         System.out.println("Sorry, try again");
      }
   }
}

Output Result

StudentMarks.java:16: error: non-static variable math cannot be referenced from a static context
   if(math >=85 && science >=75 && english >=65)
^
StudentMarks.java:16: error: non-static variable science cannot be referenced from a static context
   if(math >=85 && science >=75 && english >=65)
^
StudentMarks.java:16: error: non-static variable english cannot be referenced from a static context
   if(math >=85 && science >=75 && english >=65)
^
3 errors

To make the program work properly, either the instance variables need to be declared as static, or they should be referenced by the object in the method.

import java.util.Scanner;
public class StudentMarks {
   Scanner scan1 = new Scanner(System.in);
   private double math;
   private double science;
   private double english;
   public StudentMarks(double math, double science, double english) {
      this.math = math;
      this.science = science;
      this.english = english;
   }
   public static boolean wasPromoted(StudentMarks marks) {
      if(marks.math >=85 && marks.science >=75 && marks.english >=65)
      return true;
      return false;
   }
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your math score:");
      double math = sc.nextDouble();
      System.out.println("Enter your science score:");
      double science = sc.nextDouble();
      System.out.println("Enter your english score:");
      double english = sc.nextDouble();
      StudentMarks marks = new StudentMarks(math, science, english);
      boolean bool = wasPromoted(marks);
      if(bool) {
         System.out.println("Congratulations, you've got promoted");
      } else {
         System.out.println("Sorry, try again");
      }
   }
}

Output Result

Enter your math score:
89
Enter your science score:
85
Enter your english score:
86
Congratulations, you've got promoted
You May Also Like