javaclassstaticinstancenon-static

Can anyone tell why this error is showing up? laptop l = new laptop(); non statis to static reference


Q: Write a java program that creates a class called laptop. The data members of the class are brand (string), model(string), serial (int),colour (string), price (float), processor speed (float), RAM (int), screen size(float).Create member function that will set the individual values. Since the RAM can be upgraded therefore create a function that allows you to upgrade the RAM only. In the end, create a function that will display all the data members. You take input by using scanner and display results.

import java.util.Scanner;

class main {
    public static void main(String[] args) {
       laptop l = new laptop();
       l.display();
    }


public class laptop {
    private String brand;
    private String model;
    private int serial;
    private String colour;
    private float price;
    private float processorSpeed;
    private int RAM;
    private float screenSize;

    public void upgradeRAM(int newRAM_Installed){
        setRAM(newRAM_Installed);
    }

    public String getBrand() {
        return this.brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getModel() {
        return this.model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getSerial() {
        return this.serial;
    }

    public void setSerial(int serial) {
        this.serial = serial;
    }

    public String getColour() {
        return this.colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }

    public float getPrice() {
        return this.price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public float getProcessorSpeed() {
        return this.processorSpeed;
    }

    public void setProcessorSpeed(float processorSpeed) {
        this.processorSpeed = processorSpeed;
    }

    public int getRAM() {
        return this.RAM;
    }

    public void setRAM(int RAM) {
        this.RAM = RAM;
    }

    public float getScreenSize() {
        return this.screenSize;
    }

    public void setScreenSize(float screenSize) {
        this.screenSize = screenSize;
    }

    public void input() {
        Scanner input = new Scanner(System.in);
        //Use for repitative task 
        laptop dell = new laptop();
        System.out.println("Enter \"Brand\" name: ");
        dell.setBrand(input.nextLine());
        System.out.println("Enter \"Colour\" name: ");
        dell.setColour(input.nextLine());
        System.out.println("Enter \"Model\" name: ");
        dell.setModel(input.nextLine());
        System.out.println("Enter \"Price\" : ");
        dell.setPrice(input.nextInt());
        System.out.println("Enter \"Processor Speed\" : ");
        dell.setProcessorSpeed(input.nextFloat());
        System.out.println("Enter \"RAM\" : ");
        dell.setRAM(input.nextInt());
        System.out.println("Enter \"Screen Size\" : ");
        dell.setScreenSize(input.nextFloat());
        System.out.println("Enter \"Serial\" : ");
        dell.setSerial(input.nextInt());
        System.out.println("\n\n\\n*******Displaying 
         Result*******\n\n");
    }

    public void display() {
        laptop newLaptop = new laptop();
        newLaptop.input();
        System.out.println("Brand: "+ newLaptop.brand);
        System.out.println("RAM: "+ newLaptop.RAM);
        System.out.println("Colour: "+ newLaptop.colour);
        System.out.println("Model: "+ newLaptop.model);
        System.out.println("Price: "+ newLaptop.price);
        System.out.println("Processor Speed: "+ 
        newLaptop.processorSpeed);
        System.out.println("Screen Size: " + newLaptop.screenSize);
        System.out.println("Serial: "+ newLaptop.serial);
    }
}
}

Solution

  • When you nest a class inside another class, if that class is not marked static, it is a nested class. This means that class has a hidden reference to the instance of the outer class which created it.

    So:

    class Main {
      class Laptop {
      }
    }
    

    The Laptop class actually looks like:

    class Laptop {
      private final Main this$0;
    
      Laptop(Main this$0) {
        this.this$0 = this$0;
      }
    }
    

    All of this is hidden by the compiler, so you don't ordinarily see this$0; but the point is you actually need an instance of Main in order to be able to create an instance of Laptop.

    If you were trying to invoke new Laptop() inside a non-static method in Main, there would be no problem: the current instance ("this") would be passed as the instance of Main. But in a static method, there is no this, so there is nothing to pass.

    There are a number of ways around this:

    1. Create an instance of Main:

      Main m = new Main();
      m.new Laptop();
      
    2. Invoke new Laptop() from inside an instance method in Main (but of course, you need an instance of Main on which to invoke that method, so you'd still have to use new Main() somewhere to invoke that method.

    3. Make Laptop not a nested class: either declare it at the top-level, or make the class static. This removes the this$0 reference, so there is no need for the instance of Main any more.