Java //变量初始化错误

时间:2014-05-02 23:41:32

标签: java variables initialization

所以我正在尝试编译此代码,我得到:错误:变量笔记本电脑可能尚未启动。

public class Computer{
 String modelName;
 String motherboard;
 String systemType;
 int ram;
 int cpu
 int hdd;

 public static void main(String[] args){

Computer Laptop;
 Laptop.modelName = "M610";
 Laptop.motherboard = "MSI";
 Laptop.systemType = "Linux";
 Laptop.ram = 2048;
 Laptop.hdd = 50;
 Laptop.cpu = 1500;

System.out.println("Model name:"+Laptop.modelName);
System.out.println("Motherboard:"+Laptop.motherboard);
System.out.println("System type: "+Laptop.systemType);
System.out.println("RAM :"+Laptop.ram);
System.out.println("HDD:"+Laptop.hdd);
System.out.println("CPU :"+Laptop.cpu);
 }
}

非常感谢你!

3 个答案:

答案 0 :(得分:2)

您需要执行消息所说的内容:初始化Laptop

替换:

Computer Laptop;

使用:

Computer Laptop = new Computer();

前者声明了一个新变量,后者将其初始化。

答案 1 :(得分:1)

如前所述,你需要实例化这个类,所以你必须这样做

Computer laptop = new Computer(); // Note lower case laptop as this is how you should define variable names

你所写的内容会有所作为,但请看一下这个例子。它更像是java中的“正确方法”

public class Laptop {

    private String modelName;
    private String motherboard;
    private String systemType;
    private int ram;
    private int cpu;
    private int hdd;

    public static void main(String[] args) {        
        Laptop laptop = new Laptop();
        laptop.setModelName("M610");
        laptop.setMotherboard("MSI");
        laptop.setSystemType("Linux");
        laptop.setRam(2048);
        laptop.setCpu(50);
        laptop.setHdd(1500);

        laptop.printResult();
    }
    public void printResult() {
        System.out.println("Model name:"  + getModelName());
        System.out.println("Motherboard:" +getModelName());
        System.out.println("System type: "+ getSystemType());
        System.out.println("RAM :" + getRam());
        System.out.println("HDD :" + getHdd());
        System.out.println("CPU :" + getCpu());
    }
    public String getModelName() {
        return modelName;
    }

    public void setModelName(String modelName) {
        this.modelName = modelName;
    }

    public String getMotherboard() {
        return motherboard;
    }

    public void setMotherboard(String motherboard) {
        this.motherboard = motherboard;
    }

    public String getSystemType() {
        return systemType;
    }

    public void setSystemType(String systemType) {
        this.systemType = systemType;
    }

    public int getRam() {
        return ram;
    }

    public void setRam(int ram) {
        this.ram = ram;
    }

    public int getCpu() {
        return cpu;
    }

    public void setCpu(int cpu) {
        this.cpu = cpu;
    }

    public int getHdd() {
        return hdd;
    }

    public void setHdd(int hdd) {
        this.hdd = hdd;
    }

}

答案 2 :(得分:0)

有两种方法可以解决这个问题。

将所有字段声明为静态

public class Computer{
  static String modelName;
  static String motherboard;
  .
  .
  .

在这种情况下,不需要初始化。静态成员属于一个类而不是特定的初始化或它的实例。

将其作为计算机访问。您的文件名

但是如果你想声明一个对象。

你可以改变这一行

Computer Laptop;

到这个

Computer Laptop = new Computer();

即。在分配字段之前初始化对象'后续行中的值。

相关问题