Why can't I refer to the non-static variable in the static method?

Asked 2 years ago, Updated 2 years ago, 150 views

I didn't learn Java well from the beginning. So I don't really understand the idea of static.

An error "non-static variable cannot..." occurs when trying to declare a variable and use it within the method.

Below is a brief addition of methods for when an error occurs. Recursive call is not executed due to the above error.

I'm looking forward to helping you with grammar and how to use variables such as compareCount in your method.

public class MyProgram7 {
 public static void main (String[]args) throws IOException{
  Scanner scan = new Scanner(System.in);
  int compareCount = 0;
  int low = 0;
  int high = 0;
  int mid = 0;  
  int key = 0;  
  Scanner temp;  
  int[]list;  
  String menu, outputString;  
  int option = 1;  
  boolean found = false;  

     // // Prompt the user to select an option

    menu =  "\n\t1  Reads file" + 
       "\n\t2  Finds a specific number in the list" + 
      "\n\t3  Prints how many comparisons were needed" + 
             "\n\t0  Quit\n\n\n";

  System.out.println(menu);
  System.out.print("\tEnter your selection:   ");
  option = scan.nextInt(); 

   // // Keep reading data until the user enters 0
     while (option != 0) {
   switch (option) {

   case 1:
      readFile();
     break;

   case 2:
      findKey(list,low,high,key);
     break;

   case 3:
      printCount();
     break;

   default: outputString = "\nInvalid Selection\n";
         System.out.println(outputString);
         break;
   }//end of switch
    System.out.println(menu);
    System.out.print("\tEnter your selection:   ");
    option = scan.nextInt();
  }//end of while   
 }//end of main



 public static void readFile() {
  int i = 0;
  temp = new Scanner(new File("CS1302_Data7_2010.txt"));
  while(temp.hasNext()) {
   list[i]= temp.nextInt();
   i++;
  }//end of while
  temp.close();
  System.out.println("File Found...");
 }//end of readFile()

 public static void findKey() {
  while(found!=true) {
   while(key < 100 || key > 999) {
    System.out.println("Please enter the number you would like to search for? ie 350: ");
    key = scan.nextInt();
   }//end of inside while
    //base
   if (low <= high) {
    mid = ((low+high)/2);
    if (key == list[mid]) {
     found = true;
     compareCount++;
    }//end of inside if
   }//end of outside if
   else if (key < list[mid]) {
    compareCount++;
    high = mid-1;
    findKey(list,low,high,key);
   }//end of else if
   else {
    compareCount++;
    low = mid+1;
    findKey(list,low,high,key);
   }//end of else
  }//end of outside while
 }//end of findKey()

 public static void printCount() {
  System.out.println("Total number of comparisons is: " + compareCount);
 }//end of printCount
}//end of class

java non-static compiler-errors variable

2022-09-21 21:50

1 Answers

Make sure you understand the difference between the class and its objects. Suppose you saw a car on the street. Even if you didn't see the model or type of car, you would have known it was a car. The reason is that we compare what we see with what we see with the definition of the class of "tea." The class defines what all cars have in common. In other words, think of the class as a template. The class defines the joint data and functions of objects.

Cars seen on the road are objects in class "car". This is because, as defined in class "car," street cars have engines and wheels, and they provide the ability to drive them.

If the class defines "all cars have colors," then the object is "my color is red."

In the world of object-oriented programming, classes define fields that are variables that can store Color. When you create an object with a class, it allocates memory to store Color values and allocates Color values only for the object. These fields are called object-dependent non-static fields because they represent unique data for each object.

The static field and method are shared by all objects. They are class dependent and not object dependent. Functional methods such as Integer.parseInt() are usually defined as static methods. Usually, we define many constants as static fields. For example, a static field defines constants with limited values that do not change like the type of car.

To solve the problem of the question, you need to create an object for the class. To allocate memory to store data of objects while running the program.

Define the start of the program as follows.

public static void main (String[] args)
{
    try
    {
        MyProgram7 obj = new MyProgram7 ();
        obj.run (args);
    }
    catch (Exception e)
    {
        e.printStackTrace ();
    }
}

// // instance variables here

public void run (String[] args) throws Exception
{
    // // put your code here
}

Implement the main() method to create an object of class MyProgram7 and to invoke the method of the created object (run()).


2022-09-21 21:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.