• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Packages etc.

 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic