title: Java: How to access an instance variable in a ternary operator?
body: <p>This is the ListNode class:</p>
  
  <pre><code>  public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }
  </code></pre>
  
  <p>I am trying to initialize a PriorityQueue like this:</p>
  
  <pre><code>PriorityQueue&lt;ListNode&gt; heap = new PriorityQueue(lists.length,
              (l1, l2) -&gt; l1.val &lt; l2.val ? -1 :
                          l1.val == l2.val ? 0 :
                          1);
  </code></pre>
  
  <p>But I am getting "cannot find symbol: variable val". What is the proper way to do this? I've tried casting l1 and l2 as ListNode's, but this does not do anything.</p>
  
  <p>Edit: Why does this work?</p>
  
  <pre><code>    PriorityQueue&lt;ListNode&gt; queue= new PriorityQueue&lt;ListNode&gt;(lists.size(),new Comparator&lt;ListNode&gt;(){
              @Override
              public int compare(ListNode o1,ListNode o2){
                  if (o1.val&lt;o2.val)
                      return -1;
                  else if (o1.val==o2.val)
                      return 0;
                  else 
                      return 1;
              }
          });
  </code></pre>