从静态方法访问非静态变量[Java]

时间:2015-11-06 03:48:38

标签: java static-methods

我想做的是在静态方法中引用非静态变量。我这样做,所以我可以有一种主控制,我只需运行各个方法,使我的代码更整洁。如果有其他方法可以轻松和/或有效地完成此操作。谢谢你的时间。

select emp_id,
       sum(datediff(greatest(leave_from, '2015-09-01'),
                    least(leave_to, '2015-09-30')) + 1)
from mytable t
where leave_from <= '2015-09-30' and leave_to >= '2015-09-01'
group by emp_id

控制台错误日志:

import static java.lang.System.*; 
import java.util.Scanner;

public class InteractiveStory
{
   private int health, sanity, pumpkinPies, dexte, stren, chari, intel;
   private double money;
   private char Gender;
   public static void main(String args[]) // Main chunk of programming, preferably /only/ method refrences. TL;DR, Master Controller
    {
      genPlayer();
      //By the by, need to figure out how to access non-static variables from a static context
    }
   public static void genPlayer() //Begins the character setup bit, gender, name, class, ect.
   {
      Scanner keyboard = new Scanner(System.in); // Initalises the keyboard intepreter
      System.out.println("You are a random bystandard. You are currently sitting in a swively chair infront of a computer. What is your gender?");
      System.out.print("> ");
      String Gender = keyboard.next();
      System.out.println("");

      if(Gender.equalsIgnoreCase("female")) // need to do female first because fe'male'. recognises that even in the middle of strings
      {
         System.out.println("Okay ma'am, what're your skills?");
         Gender = "f";
      }
      else if(Gender.equalsIgnoreCase("male"))
      {
         System.out.println("Okay sir, what're your skills?");
         Gender = "m";
      }
      else if(Gender != "male")
      {
         System.out.println("Oops! Invalid gender. Sorry.");
         System.out.println();
         genPlayer();
      }
      System.out.println("Are you,");
      System.out.println("1. Dextrious?");
      System.out.println("2. Strong?");
      System.out.println("3. Charasmatic?");
      System.out.println("4. Intelligent?");
      System.out.print("> ");
      int answer = keyboard.nextInt();
      System.out.println("");

      switch(answer){
         case 1:
            dexte = dexte+1; //Errors begin here, trying to reference a private variable that is nonstatic above.
            System.out.println("You are now slightly faster than your average turtle!");
            break;

         case 2:
            stren = stren+1;
            System.out.println("You can now lift 3 pounds extra! Now a whopping 4 pounds!");
            break;

         case 3:
            chari = chari+1;
            System.out.println("You no longer scare small children!");
            break;

         case 4:
            intel = intel+1;
            System.out.println("You now have a basis for flaunting your itnelligence to rocks!");
            break;

         default:
            System.out.println("Sorry, not a valid answer!");

      }
   }
}

1 个答案:

答案 0 :(得分:0)

制作getPlayer方法non static并将其称为

    public static void main(String args[]) 
    {
      InteractiveStory interactiveStory = new InteractiveStory();
      interactiveStory.genPlayer();
    }

另见