0

Hi guys I've just learned a little Java then I faced this problem. Why does it go wrong with error "non-static variable this cannot be referenced from a static context" when I erase the "static"s in those first variables? Here is my code:

import java.util.Random;
public class Assignment1 {
        public static String [] firstName =  {"Long","Hung","Dung","Ngoc","Duc","Mai","Hoa","Lan","Tuan","Manh","Trung","Anh","Truong","Khang","Khuong","Sang","Nam","Luong","Ha","Tung","Quan","Phong","Quang","Kien","Minh"};
        public static String [] midName = {"Quoc","Tai","Trung","Dinh","Xuan","Bao","Dinh","Thi","Van","Duy"};
        public static String [] lastName = {"Nguyen","Bui","Tran","Pham","Le","Ly","Ho","Dinh","Ta","Hoang"};

        public static Random r = new Random();
        public static String [] name = new String[15];
        public static int[] id = new int[15];
        public static String [] date = new String[15];
        public static String [] rank = new String[15];

    public static void getName (){
        for(int i = 0; i < 15; ){
        int a = r.nextInt(19) + 0;
        int b = r.nextInt(9) + 0;
        int c = r.nextInt(9) + 0;
        name[i] = lastName[c]+ " " + midName[b]+" "+firstName[a];

        int key = 0;
        for(int j = i-1; j > -1; j--) //avoid repetition 
            if(name[i].compareTo(name[j]) == 0) {key++; break;}
        if(key == 0)i++;
        }   
    }
    public static void getId (){
        for(int i = 0; i <15; ){
            int d = r.nextInt(9999) + 1000;
            id[i] = d;

            int key = 0;
            for(int j = i-1; j > -1; j--)//avoid repetition 
                if(id[i] == id[j]) {key++;break;}
            if(key==0) i++;         
        }       
    }
    public static void getDate(){
        for(int i = 0; i <15; ){
            Integer month = r.nextInt(12) + 1;
            Integer day;
            switch(month){
                case 1: day = r.nextInt(31) +1;
                case 3: day = r.nextInt(31) +1;
                case 5: day = r.nextInt(31) +1;
                case 7: day = r.nextInt(31) +1;
                case 8: day = r.nextInt(31) +1;
                case 10: day = r.nextInt(31) +1;
                case 12: day = r.nextInt(31) +1;
                case 2: day = r.nextInt(28) +1;
                default: day = r.nextInt(30) +1;
            }
            date[i] = day.toString()+"/"+month.toString()+"/1997";
            int key = 0;
            for(int j = i-1; j > -1; j--)//avoid repetition 
                if(date[i].compareTo(date[j]) == 0) {key++;break;}
            if(key==0) i++;         
        }
    }
    public static void main(String [] args){
        getName();
        getId();
        getDate();
        for(int i = 0; i<15; i++){
            System.out.println(name[i] +"_"+ id[i] +"_"+ date[i]);
        }
    }
}

Many thanks!

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • 1
    Hi and welcome to SO. Please do a little research before posting a question, which has been asked many, many times. Thanks. – OldProgrammer Aug 02 '16 at 15:34
  • 1
    Java has objects, which have their own instances of each field. Static means it belongs to the class, and the reference is shared between all object instances. So it wouldn't make sense for you to reference a random variable from an object you know nothing about. – Rogue Aug 02 '16 at 15:34
  • 1
    If you're using a static method, that means that the method does not have an instance of your class that it is associated with. Therefore, it cannot access any instance variables of the class you're working with, only static once. When you call a static method, you're calling it in what Java is referring to as "static context", meaning that in the context you're working in from the method, you only have access to static variables. – James Aug 02 '16 at 15:35

0 Answers0