I am retrieving values from database. I initialized a variable outside the loop. I am using while loop to move one record after other to last using ResultSet object.next(). Inside the while I am assigning the record value to the variable initialized outside while. But It shows variable might not have been initialized error inside while and in succeeding code wherever that variable is used.
If I move them as class variable outside the method too i got error "Non Static variable cannot be initialized from static context".

Then you may have declared the variable outside the loop, but you didn't initialise (i.e. define) it. IOW if you do

String bogus;
while (true) {
  if (bogus.equals("Whatever")) {
    break;
  }
}

(as a contrived example)
you will get that compiler message, whereas with

String bogus = ""; // or even null
while (true) {
  if (bogus.equals("Whatever")) {
    break;
  }
}

you won't. Do you see the difference?

ya.I can. But i am using array.

int i=0; String bogus[i]="";

This statement preceding the while loop also shows error.

Am i did any syntax error in that?
It shows

int i=0; String bogus[i]="";

shows "] expected
; expected"
error.

This is an error:

int i = 0;
String bogus[i]= "";

Perhaps you wanted this?

int i = 0;
String bogus[] = new String[10]
bogus[i]= "";

This line would be an error no matter where you put it:

String bogus[i] = "";
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.