Jeremy Thomerson

Greenhorn
+ Follow
since Feb 17, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jeremy Thomerson

True, you can only have one public class per file. What you need to do is put all your public classes each in their own file, the name of which should be identical to the name of the class. Make sure they are also in the same directory, and that the package name is the same in each. Here's an example that I hope will help.

// My first class:
package test;

public class Test1 {

public Test1() {
super();
System.out.println("hi, I'm test 1");
}

public static void main(String[] args) {
Test1 one = new Test1();
Test2 two = new Test2();
}
}



// My second class:

package test;
public class Test2 {

public Test2() {
super();
System.out.println("Hi, I'm Test2");
}
}


These files would need to be in a directory named "test", because the directory name must match the package name, which in the case of these two classes is test. (If the package were "package test.foo", the directory would need to be {your-root}\test\foo\Test.java)

Then, from the command line you can compile and run these classes.
(My files are both in C:\home\examples\java\test)

compile any files ending with the java extension:
C:\home\examples\java>javac .\test\*.java

run the class test.Test1 (test is the package, Test1 the class)
-cp . means that your classpath is set to the directory you are in, so when you specify package test, class Test1, it will look for the compiled Test1.class file in {your-current-directory}\test\Test1.class
C:\home\examples\java>java -cp . test.Test1
hi, I'm test 1
Hi, I'm Test2

C:\home\examplesjava>

Hope this helps. let me know if it doesn't, or you have any questions.
19 years ago
I'm definitely not sure that I understand the question your asking or the use-case that you are describing. However, you are indicating that you want the call to "getInstance()" or similar to throw an exception if that instance is in use. If this is correct, would it be feasible to simply create an object pool that contains only one object? The commons project allow easy object pool creation / use. Then you could say "borrowObject()", get the object, use it, and then "returnObject(obj)" when you are done. Any calls to "borrowObject()" while the one instance in the pool is being used would result in an exception.

Not sure if this helps at all, but hopefully it will. Also, those may not be the exact method names, but they illustrate the point.
[ February 17, 2005: Message edited by: Jeremy Thomerson ]
Nice looking site. I did notice that the links on your "headlines" page seemed evasive, hard to click.
19 years ago