Java变量未初始化错误 [英] Java variables not initialized error
本文介绍了Java变量未初始化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的Java程序中收到以下错误:
I am getting the following error in my Java program:
Java变量未初始化错误...错误:变量nam和r not
initialization location class child
Java variables not initialized error... error : variable nam and r not initialized location class child
但 nan
和 r
已经初始化,但我仍然得到相同的错误。
But nan
and r
already initialized, yet I am still getting the same error.
public class cla {
int rn;
String name;
void get(String a, int x) {
name = a;
rn = x;
}
void display() {
System.out.println("student name: " + name + "roll no.:" + rn);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class child {
public static void main(String args[]) throws IOException {
String nam;
int r;
BufferedReader bReader = new BufferedReader(new InputStreamReader(
System.in));
try {
System.out.println("enter name and roll no.");
nam = bReader.readLine();
r = Integer.parseInt(bReader.readLine());
} catch (Exception e) {
}
cla c = new cla();
c.get(nam, r);
c.display();
}
推荐答案
局部变量
未获得默认值,应在使用它们之前初始化它们,初始化 nam
和 r
默认值在你的主内,你会没事的。
Local variables
don't get default values, you should initialize them before you use them , initialize nam
and r
with default values inside your main and you will be fine.
public static void main(String args[]) throws IOException{
String nam=null;
int r=0;
请注意,将类和变量命名为有意义。
BTW, consider naming your class's and variables something meaningful.
这篇关于Java变量未初始化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文