Java Code Errors: Fixing Nested Class Access and Method Usage
There are several errors in this code:
- The 'printIn' method should be 'println'.
- The 'Nested' class is missing a closing parenthesis after the class declaration.
- The 's' variable is not static and cannot be accessed from the 'Nested' class.
Here is the corrected code:
public class Test {
private static int value = 20;
public static int temp = 10;
public static class Nested {
private void display() {
System.out.println(temp + value);
}
}
public static void main(String[] args) {
Test.Nested inner = new Test.Nested();
inner.display();
}
}
This will print out the value 30, which is the sum of the static variables 'temp' and 'value'. The non-static variable 's' cannot be accessed from the 'Nested' class.
原文地址: https://www.cveoy.top/t/topic/mXED 著作权归作者所有。请勿转载和采集!