主Java中的全局变量

时间:2016-01-14 06:59:16

标签: java variables

我有以下问题,并知道为什么这会给我一些问题。我想知道在市场阵列的声明中是否有某种方式可以使用?

function chkMethode(event)
{
  if (<<certain condition>>)
  {
    event.preventDefault();
    return false;
  }
}

<a href="http://www.google.com" onclick="return chkMethode(event)"> Go </a>
很多!

3 个答案:

答案 0 :(得分:7)

你可以在main方法中初始化数组(虽然它不能final):

 static int[] market;
 public static void main(String[] args) {
     int nmarked=3;
     market = new int[nmarked];
 }

答案 1 :(得分:0)

这是对代码的更好看法:

 public static void main(String[] args) {
     int [] market = new int[3];
     for (int i=0;i<3;i++) market[i]=i;
     Consume_order consume = Consume_order(market);
     consume.start();

 }

 public class Consume_order extends Thread {
   public run(){
      private int[] array;
      public Consume_order(int[] array) {
           this.array=array;
      }
      System.out.println(array[0]);
 }

}

答案 2 :(得分:-1)

你想通过nmarket吗?你不需要通过它,使用:market.length

或者:

public class Market {
         final int[] market;
         public Market(int nmarked) {
             market = new int[nmarked];
         }
}

// from main you can call it:

Market market = new Market(3);