Java 错误 - 内部类中的非法静态声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19481680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error - Illegal static declaration in inner class
提问by user2898828
What does this mean?
这是什么意思?
Illegal static declaration in inner class Mobile.mymobile
modifier 'static' is only allowed in constant variable declarations
Line 75, public static void main(String[] args) {
My code:
我的代码:
/**
* to write a simple java class Mobile that models a mobile phone.
*
* @author (jamal)
* @version (14/10/13)
*/
public class Mobile
{
// type of phone
private String phonetype;
// size of screen in inches
private int screensize;
// menory card capacity
private int memorycardcapacity;
// name of present service provider
private String serviceprovider;
// type of contract with service provider
private int typeofcontract;
// camera resolution in megapixels
private int cameraresolution;
// the percentage of charge left on the phone
private int checkcharge;
// wether the phone has GPS or not
private String GPS;
// instance variables - replace the example below with your own
private int x;
// The constructor method
public Mobile(String mobilephonetype, int mobilescreensize,
int mobilememorycardcapacity,int mobilecameraresolution,
String mobileGPS, String newserviceprovider) {
this.phonetype = mobilephonetype;
this.screensize = mobilescreensize;
this.memorycardcapacity = mobilememorycardcapacity;
this.cameraresolution = mobilecameraresolution;
this.GPS = mobileGPS;
// you do not use this ones during instantiation,you can remove them if you do not need or assign them some default values
//this.serviceprovider = newserviceprovider;
//this.typeofcontract = 12;
//this.checkcharge = checkcharge;
Mobile samsungPhone = new Mobile(
"Samsung" // String mobilephonetype
, 1024 // int mobilescreensize
, 2 // int mobilememorycardcapacity
, 8 // int mobilecameraresolution
, "GPS" // String mobileGPS
, "verizon" // String newserviceprovider
);
//typeofcontract = 12;
//checkcharge = checkcharge;
}
// A method to display the state of the object to the screen
public void displayMobileDetails() {
System.out.println("phonetype: " + phonetype);
System.out.println("screensize: " + screensize);
System.out.println("memorycardcapacity: " + memorycardcapacity);
System.out.println("cameraresolution: " + cameraresolution);
System.out.println("GPS: " + GPS);
System.out.println("serviceprovider: " + serviceprovider);
System.out.println("typeofcontract: " + typeofcontract);
}
/**
* The mymobile class implements an application that
* simply displays "new Mobile!" to the standard output.
*/
public class mymobile {
public static void main(String[] args) {
System.out.println("new Mobile!"); //Display the string.
}
}
public static void buildPhones(){
Mobile Samsung = new Mobile("Samsung", 3, 4, 8, "verizon", "GPS");
Mobile Blackberry = new Mobile("Blackberry", 3, 4, 8, "verizon", "GPS");
}
public static void main(String[] args) {
buildPhones();
}
}
回答by Rajnish Mishra
You can not have a public and static field inside a static inner class
静态内部类中不能有公共和静态字段
See the Inner Class Documentation here
回答by jantzl
This is well answered here: Modifier static is only allowed in constant variable declarations
这在这里得到了很好的回答: Modifier static is only allowed in constant variable declaration
In short: because mymobile is a subclass of Mobile, it cannot have a static member (the main function).
简而言之:因为 mymobile 是 Mobile 的子类,所以它不能有静态成员(主函数)。
If you can move mymobile class out of Mobile, the error should clear.
如果您可以将 mymobile 类移出 Mobile,则错误应该会清除。
回答by Niks
In Java it's not possible to add two public class in one file.you need to changemymobile
class modifier default like class mymobile
or you can change Mobile
class modifier to default like class Mobile
.
在 Java 中,不可能在一个文件中添加两个公共类。您需要更改mymobile
类修饰符 default likeclass mymobile
或者您可以将Mobile
类修饰符更改为 default like class Mobile
。