java - 错误 - 内部类中的非法静态声明

标签 java

这是什么意思?

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) {  

我的代码:

/**
 * 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();
    }  
}

最佳答案

这里回答得很好: Modifier static is only allowed in constant variable declarations

简而言之:因为mymobile是Mobile的子类,所以不能有静态成员(main函数)。

如果您可以将 mymobile 类移出 Mobile,错误应该会清除。

关于java - 错误 - 内部类中的非法静态声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19481680/

相关文章:

java - 如何从 src/main/java 中的 src/test/resources 读取 props

java - 创建 BufferedImage 后元数据丢失

java - 尝试使用 Spring JdbcTemplate 从 Java 访问 OS400/DB2 存储过程输出参数

Java - 函数式编程(映射、过滤器)

java - 给新变量赋值?

java - Springfox Swagger 一代创建了奇怪的模式

java - 线程安全读/写计数器

java - 为什么 PrintStream.close() 最终会被调用两次?

java - 从 EJB 或 Java EE webapp 使用 http 资源的最佳方式

java - 解释 Java 单词“synchronized”在处理草图中的使用