문제

so i am trying to compile this code and i get : ERROR : variable Laptop might not have been initiated.

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);
 }
}

Thank you very much in advance !

도움이 되었습니까?

해결책 2

Yup as have been said, you need to instantiate the class so you have to do

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

What you have wrote will do, but have a look at this example. Its more of a "correct way" in 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;
    }

}

다른 팁

You need to do what the message says: Initialize Laptop.

Replace:

Computer Laptop;

With:

Computer Laptop = new Computer();

The former declares a new variable which the latter initializes it.

There are two ways to solve this problem.

Declare all your fields as static

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

In this case there is no need of initialization. Static members belong to a class rather than to a specific initialization or an instance of it.

Access them as Computer.your-filedname.

However if you want to declare an object.

You can just change this line

Computer Laptop;

to this

Computer Laptop = new Computer();

i.e. you initialize your object before assigning your fields' values in subsequent lines.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top