0

I am just starting to use Java. I am using NetBeans and inside my .pkg1 file I have two .java files. I am doing the Coursera course on Algorithms by the way, so my code references that:

CourseraAlgorithmsWeek1.java

package coursera.algorithms.week.pkg1;

public class CourseraAlgorithmsWeek1 {
    public static void main(String[] args) {
        QuickFindUF mystuff(10); // DOES NOT WORK!
    }  
}

QuickFindUF.java

public class QuickFindUF {
    private int[] id;

    public QuickFindUF(int N){
        id = new int[N];
        for(int i =0; i< N; i++){
            id[i] = i;
        }
    }
}

My problem is that the first line in my main function does not recognize the QuickFindUF object creation. I read that I need to compile the second file into a .class file, and then into a .jar file. How can I do this with netbeans?

I also read a bit about the Classpath. Can I only add .jar files to the classpath?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Jake Stout
  • 487
  • 1
  • 5
  • 17
  • you could put them in the same package. – Hunter McMillen Aug 14 '12 at 16:41
  • 1
    Do you have the same `package` declaration at the top of `QuickFindUF`'s file? If the package declarations don't match then you have to do an `import` to get the names in scope (unless you want to use the fully-qualified name). – DaoWen Aug 14 '12 at 16:42
  • does the QuickFindUF.java have the same package declaration? – ChadNC Aug 14 '12 at 16:42
  • Your problem is not about using 2 classes in Java but initialize a class instance. You're doing it the C++ way, [su-'s answer](http://stackoverflow.com/a/11956959/1065197) shows how to solve your problem. – Luiggi Mendoza Aug 14 '12 at 16:49
  • The first comment scratches the itch. Use the import statement if class CuickFindUF is in another package. As other commenters say, you are using C++ syntax. – mozillanerd Aug 14 '12 at 17:15

4 Answers4

7

change

QuickFindUF mystuff(10);

to

QuickFindUF mystuff = new QuickFindUF(10);
su-
  • 3,116
  • 3
  • 32
  • 42
1

Move QuickFindUF.java to the same package of your main class by adding

package coursera.algorithms.week.pkg1;

before class definition.

StepTNT
  • 3,867
  • 7
  • 41
  • 82
  • The files must be in the same directory too. – Luiggi Mendoza Aug 14 '12 at 16:42
  • He's working with Netbeans. I don't think he's got to deal with directories as the IDE manages everthing into a project, but thanks for adding this info! – StepTNT Aug 14 '12 at 16:44
  • Is a package identical to a directory for Netbeans? – Jake Stout Aug 14 '12 at 16:52
  • Yes, in JAVA packages are basically folders. If you create a new package into you NetBeans project, it will create a new folder. For example, package `org.yourcompany.utils` will be located at `../NetBeans Projects/yourproject/src/org/yourcompany/utils` – StepTNT Aug 14 '12 at 17:08
1

You can add a directory to the classpath as well. You could do something like

export CLASSPATH = "."

and that would include the directory you are currently in. This should allow you to compile and execute code in that directory.

If the classpath is set correctly, you can either move the QuickFindUF class to the coursera.algorithms.week.pkg1 package by adding this declaration at the top of the page:

package coursera.algorithms.week.pkg1;

or, you can import the class by using the declaration:

import coursera.algorithms.week.pkg1.CourseraAlgorithmsWeek1;
jcern
  • 7,798
  • 4
  • 39
  • 47
  • Is the export command in a command line interface? Or can I do that using Netbeans GUI? Thanks! – Jake Stout Aug 14 '12 at 16:51
  • That is a command line interface using bash in unix. If you are using netbeans, your classpath is most likely set - but you can verify by using these steps: http://stackoverflow.com/questions/7598623/how-to-setup-classpath-in-netbeans – jcern Aug 14 '12 at 17:12
0

1. Use Composition.

QuickFindUF  q = new QuickFindUF();
q.mystuff(10);
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75