Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot instantiate the type..."

Tags:

java

class

queue

When I try to run this code:

import java.io.*;
import java.util.*;

public class TwoColor
{
    public static void main(String[] args) 
    {
         Queue<Edge> theQueue = new Queue<Edge>();
    }

    public class Edge
    {
        //u and v are the vertices that make up this edge.
        private int u;
        private int v;

        //Constructor method
        public Edge(int newu, int newv)
        {
            u = newu;
            v = newv;
        }
    }
}

I get this error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Cannot instantiate the type Queue
    at TwoColor.main(TwoColor.java:8)

I don't understand why I can't instantiate the class... It seems right to me...

like image 560
StickFigs Avatar asked Sep 10 '25 05:09

StickFigs


1 Answers

java.util.Queue is an interface so you cannot instantiate it directly. You can instantiate a concrete subclass, such as LinkedList:

Queue<T> q = new LinkedList<T>;
like image 188
Cameron Skinner Avatar answered Sep 12 '25 19:09

Cameron Skinner