初学者常遇Java编译时错误
编译错误 | ErrorMessage |
---|---|
错误: 非法的类型开始 | illegal start of type |
错误: 需要’;’ | ‘;’ expected |
错误: 方法声明无效;需要返回类型 | invalid method declaration; return type required |
错误: 需要<标识符> | < identifier> expected |
错误: 无法从静态上下文中引用非静态 变量 | error: non-static variable s cannot be referenced from a static context |
错误: 无法从静态上下文中引用非静态 方法 | error: non-static method Print(String) cannot be referenced from a static context |
代码举例
代码尝试1.0
源码1.0
这是我根据本文契机代码写的错误情况简化代码,我的初衷是希望这个代码运行的时候,输出一个“Hello,world”,但是很不幸地我把打印字符串的命令写在了类中但是任何一个方法外面。
public class Nothing
{
private String s = "Hello,World!";
this.Print(s); // <- ERROR
private void Print(String s){
System.out.println(s);
}
}
编译1.0
一口气报了四个错误!
>javac Nothing.java
Nothing.java:4: 错误: 非法的类型开始
this.Print(s);
^
Nothing.java:4: 错误: 需要';'
this.Print(s);
^
Nothing.java:4: 错误: 方法声明无效; 需要返回类型
this.Print(s);
^
Nothing.java:4: 错误: 需要<标识符>
this.Print(s);
^
4 个错误
>javac -J-Duser.language=en Nothing.java //[1]
Nothing.java:4: error: illegal start of type
this.Print(s);
^
Nothing.java:4: error: ';' expected
this.Print(s);
^
Nothing.java:4: error: invalid method declaration; return type required
this.Print(s);
^
Nothing.java:4: error: <identifier> expected
this.Print(s);
^
4 errors
代码尝试1.1
源码1.1
然后,我想起来了,有构造函数这么一回事,应该把方法卸载构造函数里面,放进去啦,在再main里面启动这个方法,哦吼吼!
public class Nothing
{
private String s = "Hello,World!";
private void Print(String s){
System.out.println(s);
}
public Nothing(){
Print(s);
}
public static void main(String args[])
{
Nothing(); // <- ERROR
}
}
编译1.1
失败了,居然说找不到符号,这是你自己的构造函数啊,你怎么找不到啊!
>javac Nothing.java
Nothing.java:12: 错误: 找不到符号
Nothing();
^
符号: 方法 Nothing()
位置: 类 Nothing
1 个错误
>javac -J-Duser.language=en Nothing.java
Nothing.java:12: error: cannot find symbol
Nothing();
^
symbol: method Nothing()
location: class Nothing
1 error
代码尝试1.2
源码1.2
然后,我想起来任何构造函数要启动它好像一定要有一个对象…
public class Nothing
{
private String s = "Hello,World!";
private void Print(String s){
System.out.println(s);
}
public Nothing(){
Print(s);
}
public static void main(String args[])
{
Nothing n = new Nothing(); // main里面声明一个对象,自动触发构造函数
}
}
编译1.2
总算有一个通过编译,运行成功的版本了!
>javac -J-Duser.language=en Nothing.java
>java Nothing
Hello,World!
代码尝试1.3
源码1.3
反正,都写到这个份上了,那就连print()也直接来一次吧,看看编译错误信息是不是一样:
public class Nothing
{
private String s = "Hello,World!";
private void Print(String s){
System.out.println(s);
}
public static void main(String args[]){
Print(s); // <-ERROR
}
}
编译
什么?你更在乎它是不是静态的?什么,你连自己的构造函数都找不到却能找到print了?还是说不是静态更严重,print找不到找得到都无所谓了?
>javac Nothing.java
Nothing.java:9: 错误: 无法从静态上下文中引用非静态 变量 s
Print(s);
^
Nothing.java:9: 错误: 无法从静态上下文中引用非静态 方法 Print(String)
Print(s);
^
2 个错误
>javac -J-Duser.language=en Nothing.java
Nothing.java:8: error: non-static variable s cannot be referenced from a static
context
Print(s);
^
Nothing.java:8: error: non-static method Print(String) cannot be referenced from
a static context
Print(s);
^
2 errors
代码尝试1.4
源码1.4
好吧,我懂了,还是老老老实实地写符合规则的代码吧,
public class Nothing
{
private String s;
private static void Print(String s){ // 看,我给你加static了。
System.out.println(s);
}
public static void main(String args[]){
s = "Hello,World"; // 我拿到这里来赋值就没问题了吧!
Print(s);
}
}
编译1.4
真是实力不会写Java….
>javac Nothing.java
Nothing.java:10: 错误: 无法从静态上下文中引用非静态 变量 s
s = "Hello,World";
^
Nothing.java:11: 错误: 无法从静态上下文中引用非静态 变量 s
Print(s);
^
2 个错误
>javac -J-Duser.language=en Nothing.java
Nothing.java:8: error: non-static variable s cannot be referenced from a static
context
s = "Hello,World";
^
Nothing.java:9: error: non-static variable s cannot be referenced from a static
context
Print(s);
^
2 errors
代码尝试1.5
源码1.5
所以,不用构造函数的就这样写….
public class Nothing
{
private static void Print(String s){
System.out.println(s);
}
public static void main(String args[]){
String s = "Hello,World";
Print(s);
}
}
编译1.5
H E L O , W O R L D
>javac -J-Duser.language=en Nothing.java
>java Nothing
Hello,World
本文契机
出错代码
/* 不完整源码:实现一个 FIFO Queue 队列类的迭代器代码 */
import java.util.Iterator;//[3]迭代器
class Queue<Item>
{
//...
private Node first;
private class Node
{
Item item;
Node next;
}
// 假定这个可以反转链表结构并返回一个新的起点
private Node reverse(Node current){
}
/************************* 迭代器实现 ******************************/
public Iterator<Item> iterator(){
return new ListIterator();
}
// 类中类
private class ListIterator implements Iterator<Item>//[3]迭代器
{
private Node current;
current = reverse(first); // <-ERROR 这句代码就这么生生地被我写在了这里 见1.1
public boolean hasNext(){}
public Item next(){}
public void remove(){}
}
// ...
/************************* 迭代器实现 ******************************/
}
构造函数
public Iterator<Item> iterator(){
return new ListIterator();
}
private class ListIterator implements Iterator<Item>//[3]迭代器
{
private Node current;
public ListIterator()
{
current = reverse(first); // YES!
}
public boolean hasNext(){}
public Item next(){}
public void remove(){}
}
代码心得
这种问题属于Java的编程规范和语法规定吧,但是编译的时候给的提示信息也让人很困惑,写着写着就写懵了,其实我也认可这个世界上没有Stupid Code,但是底线是代码好歹要通过编译啊!一定要让自己记住这个深刻的教训。人啊,比起作对,更难是找到自己的错、改正自己的错啊!
参考引用
[1] JDK 显示英文编译信息
http://bbs.chinaunix/thread-3756137-1-1.html
[2] How do I view and change the system locale settings to use my language of choice?
http://java/en/download/help/locale.xml
更多推荐
[java]初学者java编译时错误小总结
发布评论