Can you help please. I do not know how to change the code so that this error does not occur. You do not need to use mutable objects.

public class Lab8 { class Device { String name; String model; String year; public String getName(){ return name; } public String getModel(){ return model; } public String getYear(){ return year; } public Device(String name, String model, String year){ this.name=name; this.model=model; this.year=year; } public void display(){ System.out.println("Name: " + name); System.out.println("Model: " + model); System.out.println("Year: " + year); } } class Laptop extends Device{ public Laptop(String name, String model, String year) { super(name, model, year); } } class Tablet extends Device{ public Tablet(String name, String model, String year) { super(name, model, year); } @Override public void display(){ System.out.printf("Name -> :) %s \n", getName()); System.out.printf("Model -> :) %s \n", getModel()); System.out.printf("Year -> :) %s \n", getYear()); } } class PC extends Device { public PC(String name, String model, String year) { super(name, model, year); } } public static void main(String[] args){ Laptop Laptop = new Laptop("Laptop", "ASUS", "2018"); Laptop.display(); Tablet Tablet = new Tablet("Tablet", "SONY", "2010"); Tablet.display(); PC PC = new PC("PC", "HP", "2017"); PC.display(); } } 
  • In general, either write a static class Device or bring it out from under the class Lab8 . Google by keyword inner class - pavel

1 answer 1

The whole problem is that in one class you have a main method, and class fields. If you want operations to occur in the same class / file, then the class fields should be designated as its static . Another solution to the problem is to create a separate class only with the main method, where to carry out all operations, and delete the main from Lab8.