static and non static methods in java [duplicate]

2019-05-30 22:40发布

问题:

Possible Duplicate:
When should a method be static?

i am trying to make an interface class for tube/subway ticket machine. well not for real, but for a coursework in a computer science module. i dont understand when to use static methods. i dont know much about computer sciences, but main methods seem to use static.

class UNInterfaceTest
{
   public static final int NOTTING_HILL = 1;
   public static final int HIGH_KEN = 2;
   public static final int GLOUS = 3;
   public static final int SOUTH_KEN = 4;
   public static final int SLOANE = 5;
   public static final int VICTORIA = 6;
   public static final int ST_JAMES = 7;
   public static final int WESTMINSTER = 8;
   public static final int QUIT = 10;
   private Input in = new Input();

private static void displayMenu()
{
   System.out.println("CIRCLE LINE: Please Select the Number of Your Current Station.");
   System.out.println(NOTTING_HILL + ": Nottinghill_Gate");
   System.out.println(HIGH_KEN + ": High_Street_Kensignton");
   System.out.println(GLOUS + ": Gloucester_Road");
   System.out.println(SOUTH_KEN + ": South_Kensignton");
   System.out.println(SLOANE + ": Sloane_Square");
   System.out.println(VICTORIA + ": Victoria"); 
   System.out.println(ST_JAMES + ": St_James_Park");
   System.out.println(WESTMINSTER + ": Westminster");
   System.out.println();
   System.out.println(QUIT + ". Quit");
}

public static void run()
{
    while(true)
    {
        displayMenu();
        int option = getMenuInput();
        if (option == QUIT)
    {
        break;
    }
    doOption(option);
    }
 }

private static void doOption(int option)
{ 
    switch(option){
 case NOTTING_HILL:
     //findNottinghill_Gate();
       break;
      case HIGH_KEN:
     //findHighStreetKensignton();
       break;
      case GLOUS:
     //findGloucesterRoad();
       break;
      case SOUTH_KEN:
     //findSouthKensignton();
       break;
      case SLOANE:
     // findSloaneSquare();
       break;
      case VICTORIA:
     //findVictoria();
       break;
      case ST_JAMES:
     //findStJamesPark();
       break;
      case WESTMINSTER:
      //findWestminster();
       break;
      default:
        System.out.println("Invalid option - try again");
    }
} 
private int getMenuInput()
{
    //KeyboardInput val = new KeyboardInput();
    System.out.print("Enter menu selection: ");
    int option = in.nextInt();
    in.nextLine();
    return option;
}

 public static void main(String[] args)
 {
    run();
 }
}

回答1:

Think of a class as a cookie cutter, and an object as the cookie (an instance of the class). Static methods are methods part of the class itself (the cookie cutter), as in MyClass.myMethod();, whereas non-static methods are part of the class instances (the cookies), as in new MyClass().myMethod();.

In your case, static methods would be things specific to ticket machines in general, like calculating how much money should be deducted. Non-static methods would be things specific to a single ticket counter, such as keeping track of how many tickets it has processed.

Here is some more information:
http://cscie160-distance.com/nonstatic.html



回答2:

A static method is not associated with an instance of a class.
Any method which doesn't use or modify the instance of the class it's defined on should be static.



回答3:

Short answer: That is because you are not making use of your objects. You can instantiate your class as an object and run it using

 
UNInterfaceTest unit = new UNInterfaceTest();
unit.run();

using non-static methods.



回答4:

Instance methods are per-ticket machine, while static methods do something that affects all ticket machines simultaneously.

Now, in your case, you may realistically only use on e at a time. But conceptually, each machine would have its own keyboard, screen, and CPU, so all the methods should be instance.

That means you need to create a machine in main:

UNInterfaceTest machine = new UNInterfaceTest();
machine.run();

Of course, you're currently writing directly to standard out (not sure what Input reads from). A better design might be to pass in a Writer to the UNInterfaceTest constructor, then write to that (instead of System.out). That lets each machine have its own screen.

Of course, some of this information may be beyond the scope of the assignment. It should still be useful to know.