1

Here's the function to add node in singly link list. It is getting executed on Java 15, but for Java 8 it shows the compilation error.

public static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode llist, int data, int position) {
// Write your code here
        SinglyLinkedListNode new_node = new SinglyLinkedListNode(data);
    SinglyLinkedListNode curr_node = llist;
    
    int i=0;
    while(i<position-1)
    {
        curr_node = curr_node.next;
        i++;
    }
    new_node.next = curr_node.next;
    curr_node.next = new_node;
    
    return llist;
}

Here's the link for whole problem statement: https://www.hackerrank.com/challenges/insert-a-node-at-a-specific-position-in-a-linked-list/problem

This is the error:

Solution.java:78: error: Illegal static declaration in inner class Solution.Result
    public static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode llist, int data, int position) {
                                       ^
  modifier 'static' is only allowed in constant variable declarations
Solution.java:121: error: cannot find symbol
        SinglyLinkedListNode llist_head = insertNodeAtPosition(llist.head, data, position);
                                          ^
  symbol:   method insertNodeAtPosition(SinglyLinkedListNode,int,int)
  location: class Solution
2 errors
Exit Status

1

What is the reason for this error?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Needs more code to talk about. – Joop Eggen Aug 20 '21 at 05:46
  • Does this answer your question? [Modifier static is only allowed in constant variable declarations](https://stackoverflow.com/questions/6440010/modifier-static-is-only-allowed-in-constant-variable-declarations) – enzo Aug 20 '21 at 05:48
  • The problem statement doesn't seem to have defined properly for Java 8. SinglyLinkedListNode is not defined in java 8 skeleton code – null Aug 20 '21 at 06:29
  • @enzo ... no. It is a different problem. – Stephen C Aug 20 '21 at 07:28
  • @null - this is code that the OP has written himself. `SinglyLinkedListNode` is presumably >his< class. (And the compilation error doesn't say that the compiler can't resolve `SinglyLinkedListNode`.) – Stephen C Aug 20 '21 at 07:31
  • Oh... Got it. Thanks @Stephen C for clarifying. – null Aug 20 '21 at 08:38

1 Answers1

2

The reason for the first compilation error in Java 8 was that static methods were not allowed in inner classes prior to Java 15. The restriction was removed as part of the changes to support record classes. (See Why are static methods allowed inside a non-static inner class in Java 16? for some background).

The second error is a consequence of the first one. The Java 8 compiler has rejected the method declaration ... and then won't resolve it as a symbol.

The simplest solution for Java 8 portability is to remove the static modifier from that method. (Declaring the Result class as static might be an alternative ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216