Can we make static reference to non-static fields in java?

Java 8Object Oriented ProgrammingProgramming

A class in Java will have three kinds of variables namely, static (class), instance and, local.

  • Local variables − These variables belong to and declared/defined within the methods/blocks/constructors. The scope of these variables lies within the method (or, block or, constructor) and will be destroyed after he execution of it.

  • Instance variables − These variables belong to the instances (objects) of a class. These are declared within a class but outside methods. These are initialized when the class is instantiated. They can be accessed from any method, constructor or blocks of that particular class.

    You must access instance variables using an object. i.e. to access an instance variable you need to create an object of the class and using this object you need to access these variables.

  • Class/static variables − class/static variables belong to a class, just like instance variables they are declared within a class, outside any method, but, with the static keyword.

    They are available to access at the compile time, you can access them before/without instantiating the class, there is only one copy of the static field available throughout the class i.e. the value of the static field will be same in all objects. You can define a static field using the static keyword.

Static reference e to non-static variables

As discussed above Static variables are referred (accessed) using the class name.

System.out.println(MyClass.data);

i.e. referring a variable using static reference implies to referring using the class name.

But, to access instance variables it is a must to create an object, these are not available in the memory, before instantiation.

Therefore, you cannot make static reference to non-static fields(variables) in Java. If you still, try to do so a compile time error is generated saying “non-static variable math cannot be referenced from a static context”.

Example

Following Java program, accepts marks from user and decides whether he got promoted.

Here we from the static method wasPromroted(), we are accessing the instance variables directly (just by specifying their name, like we do with static variables). Since it is not allowed this will generate a compile time error.

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 wasPromroted(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 = wasPromroted(marks);
      if(bool) {
         System.out.println("Congratulations you've got promoted");
      } else {
         System.out.println("Sorry try again");
      }
   }
}

Output

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

Solution

To make this program work, either we need to declare the instance variables static or, we should refer them using an 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 wasPromroted(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 = wasPromroted(marks);
      if(bool) {
         System.out.println("Congratulations you've got promoted");
      } else {
         System.out.println("Sorry try again");
      }
   }
}

Output

Enter your math score:
89
Enter your science score:
85
Enter your english score:
86
Congratulations you've got promoted
raja
Published on 05-Aug-2019 17:46:16
Advertisements