mvb
mvb

Reputation: 701

Refer class in another .java file in my program

I am absolutely new to Java and having trouble referencing a class that I have declared in another .java file. I read a lot about it on the Oracle Website and also many stackoverflow questions, but couldn't solve the problem. Here are the steps I have taken:

Set the class path. I am running this on UNIX. So I typed in % java -classpath /users/myUserName/algs4/assignment1. Instead of changing the class path it gives me a long list of other options.

Include the 'package name' in each of my .java files which have class definitions I want to refer to.

I also ensured that all my files that I want to refer are in the same directory. Here is some of my code:

package assignment1;
public class Percolation
{
    private int[][] grid;
    ....
    //Other method definitions etc.
   public Percolation(int N)
   {
      uf = new WeightedQuickUnionUF(N);
      grid = new int[N][N];
      ...
      //Other code

   }

 }

My WeightedQuickUnionUF class is also defined as:

package assignment1;
public class WeightedQuickUnionUF {
    private int[] id;    // id[i] = parent of i
    private int[] sz;    // sz[i] = number of objects in subtree rooted at i
    private int count;   // number of components

    //Create an empty union find data structure with N isolated sets.
    public WeightedQuickUnionUF(int N) {
        count = N;
        id = new int[N];
        sz = new int[N];
        for (int i = 0; i < N; i++) {
            id[i] = i;
            sz[i] = 1;
        }
     }
}

But now when I compile using javac Percolation.java. It gives me the errors:

Percolation.java:7: cannot find symbol
symbol  : class WeightedQuickUnionUF
location: class assignment1.Percolation
WeightedQuickUnionUF uf;
^

I get it that it is because the compiler doesn't know what is the class WeightedQuickUnionUF. It cannot refer it. But how do I do it then? I have already tried the popular solutions.

I am not using any IDE. Just a text editor and then compiling it on the terminal.

(algs4 folder has files like stdlib.jar and others)

EDIT: I missed the 'new' keyword. I have added that. Also I am compiling my WeightedQuickUnionUF class before Percolation.java

Upvotes: 1

Views: 3501

Answers (3)

dcernahoschi
dcernahoschi

Reputation: 15240

The java command is not setting the classpath but trying to run a java class. That's why the long option list.

You can specify the classpath upon compilation: run javac -classpath . assignment1/Percolation.java from the algs4 directory. Pay attention to the dot it means current directory.

There is no need to compile the files in order.

To run your program: java -cp . assignment1.Percolation from the algs4 directory.

Upvotes: 2

Kazekage Gaara
Kazekage Gaara

Reputation: 15052

You need to first compile the dependencies, ie, in your case the WeightedQuickUnionUF class.

And if you want an anonymous object, declare it along with the new keyword :

uf = new WeightedQuickUnionUF(N);

Or use it as mentioned in another answer.

Since that answer is deleted(I don't see it anymore), the other way around is to have a named object:

WeightedQuickUnionUF myObj; 

..

myObj = new WeightedQuickUnionUF(N);
uf = myObj;

Upvotes: 1

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

You must compile the classes in the

order of the flow of the Object Graph.

That means Percolation class CANNOT be compiled before the WeightedQuickUnionUF get compiled.

So first compile the WeightedQuickUnionUF then Percolation.

I think you have even missed out the new keyword...while initializing WeightedQuickUnionUF(N);

uf = new WeightedQuickUnionUF(N);

Upvotes: 0

Related Questions