package array1;
import javax.swing.*;
import java.util.Random;
class pickrand{
    Random rand = new Random();

public static String get(String[] array){
int rnd = rand.nextInt(array.length);
          return array[rnd];       
    }
}
/*********************************************************************/
public class Array1 {

public static void main(String[] args) {

    int size = 10;
       String[] passcheck = new String[size];
       String[] pass = {"abc345", "7865bn", "987ghw", "234hjg", "060j34", "likh33", "magg33", "alp335", "kawr87", "78j234" } ;
  String ret = pickrand.get(pass);
 JOptionPane.showMessageDialog(null,"your password is: \n" + ret);
 }

 }

`

iam new in java and facing problem with non-static variable try to help me thnx...... 
error is this 
 Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static variable rand cannot be referenced from a static context
    at array1.pickrand.get(Array1.java:8)
    at array1.Array1.main(Array1.java:20)Inline Code Example Here

`

Recommended Answers

All 2 Replies

The variable: rand only exists when there is a instance of the Pickrand class. It is not defined as static.
The code is trying to access it without first creating an instance of the class and of the variable. If it were static you would not need an instance of the class to access it from a static method.

problem resolved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.