File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Packages etc. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Packages etc." Watch "Packages etc." New topic
Author

Packages etc.

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
I thought I had finally mastered Packages and imports but I'm having a problem with this piece of code.

import com.pack.*;
public class Testing
{
public static void main (String args[])
{
OtherClass oc = new OtherClass()
}
}

This errors when I compile it at the point where it says "new OtherClass()". The OtherClass class is in the com.pack packages so it must be finding it. I'd swear it was a path problem but if I simply have "OtherClass oc = null", it compiles so it IS seeing the OtherClass class.
Can anyone give me an idea on what I'm missing. If I remove the package reference in OtherClass and compile it into my current directory, it all works.
In summary : how can a class 'see' another class when you say "OtherClass oc = null" but not 'see' it when you try to instantiate it by typing "OtherClass oc = new OtherClass()"?
Any help, as usual, is much appreciated.
Thanks.
Paul
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
Hi,
I entered the below code and it ran fine. Be careful with accessiblity in OtherClass. Since it is declared in a different package, I defined it as public. Also, a semicolon was added as noted in the code.
To compile I did the following:
//The -d creates the directories if they are not there
javac -d . OtherClass.java
javac Testing.java
java Testing
Also, you could test OtherClass by running it.
import com.pack.*;
public class Testing
{
public static void main (String args[])
{
OtherClass oc = new OtherClass(); // Semicolon was missing
}
}
// File OtherClass
package com.pack;
public class OtherClass {
public OtherClass() { System.out.println("OtherClass constructor"); }
public static void main(String args []) { OtherClass o1 = new OtherClass(); }
}
I hope this helps.
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
My mistake in message. I did have the semicolon in my code so that was not the problem. I am compiling using a tool called JCreator which I find very useful - although limited.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Packages etc.