1

I'm studying linked lists from this video from UC Berkeley. However, when I try typing the same version in my Eclipse compiler, like this....

public class CreateLinkedList{

    public class Node{
        String PlayerName;
        Node next;
    }


    Node first = new Node();
    Node second = new Node();
    Node third = new Node();

    first.PlayerName = "Sanchez";
    second.PlayerName = "Ozil";
    third.PlayerName = "Welbeck";


    public static void main(String[] args) {

    }
}

I get the error "Syntax error on token "PlayerName", VariableDeclaratorID expected after this token" on the following lines

first.PlayerName = "Sanchez";
second.PlayerName = "Ozil";
third.PlayerName = "Welbeck";

Can anyone explain where I'm going wrong?

satnam
  • 1,457
  • 4
  • 23
  • 43

4 Answers4

2

This code

Node first = new Node();
Node second = new Node();
Node third = new Node();

first.PlayerName = "Sanchez";
second.PlayerName = "Ozil";
third.PlayerName = "Welbeck";

is not in a code block, try moving it to main.

Also the Node class will need to static, or else move it to a seperate .java file.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
2

You can't initialize like this

first.PlayerName = "Sanchez";
second.PlayerName = "Ozil";
third.PlayerName = "Welbeck";

You have to move it to some of the methods of class CreateLinkedList If you are putting it in main you have to declare the Node instances static

static Node first;
static Node second;
static Node third;
Johny
  • 2,128
  • 3
  • 20
  • 33
  • Okay, that works. But any reason why they have to be initialised inside a method? Also, why isn't the professor in the video doing the same? – satnam Oct 03 '14 at 03:45
2

Review nested class definitions... If you want to set the properties from a static context... you need a static class.

public class CreateLinkedList{

static class Node{
    String playerName;
    Node next;
}

public static void main(String[] args) {

    Node first = new Node();
    Node second = new Node();
    Node third = new Node();

    first.playerName = "Sanchez";
    second.playerName = "Ozil";
    third.playerName = "Welbeck";

    System.out.println("First is : " + first.playerName);
    System.out.println("Second is : " + second.playerName);
    System.out.println("Third is : " + third.playerName);

    }
}

If you wish to keep your inner class as a nested public class then you need to instantiate the upper class first.

public class CreateLinkedList {

    public class Node {
        String playerName;
        Node next;
    }

    public static void main( String[] args ) {

        Node first = new CreateLinkedList().new Node();
        Node second = new CreateLinkedList().new Node();
        Node third = new CreateLinkedList().new Node();

        first.playerName = "Sanchez";
        second.playerName = "Ozil";
        third.playerName = "Welbeck";

        System.out.println( "First is : " + first.playerName );
        System.out.println( "Second is : " + second.playerName );
        System.out.println( "Third is : " + third.playerName );

    }
}
Community
  • 1
  • 1
Edward J Beckett
  • 5,061
  • 1
  • 41
  • 41
1

shouldn;t these lines be in main() function :

Node first = new Node();
Node second = new Node();
Node third = new Node();

first.PlayerName = "Sanchez";
second.PlayerName = "Ozil";
third.PlayerName = "Welbeck";
Satya
  • 8,693
  • 5
  • 34
  • 55