• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple classes in one source code file  RSS feed

 
Jeff Tian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.

I have placed 1 public and 1 default class in a file. The name matches the public class name.

class OtherClass{
public static void main(String[] args){
System.out.println("Inside OtherClass main");
}
}
public class ClassTester{
public static void main (String[] args) {
System.out.println("Inside ClassTester main");
}
}

I had intended for the JVM to execute the public class and not the default "OtherClass".

It appears that the JVM will just execute the whichever class comes first, in this case, the "OtherClass".

How do I make it execute the main in ClassTester first, without moving ClassTester on top of "OtherClass".

_________________________
BTW

If you are wondering why, I was writing exceptions within one source code file and thought that the public class would be executed first:

class AnotherException{
public static void main(String[] args){
System.out.println("Another Exception thrown");
}
}

class MyExceptionThree extends MyExceptionTwo{
public static void main(String[] args){
System.out.println("Exception 3 thrown");
}
}

class MyExceptionTwo extends MyExceptionOne{
public static void main(String[] args){
System.out.println("Exception 2 thrown");
}
}

class MyExceptionOne extends Exception{
public static void main(String[] args){
System.out.println("Exception 1 thrown.");
}
}

public class TestMyExceptions{
public static void main(String[] args) throws MyExceptionTwo, IOException
{
TestMyExceptions tester1 = new TestMyExceptions();
// Code that will cause some of my exceptions to be thrown
}
}
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you'r positive you saved your file as ClassTester.java?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the JVM executes classes, not source files. From the command line, you tell the "java" program the name of a class:

java ClassTester

and it finds the file ClassTester.class, and calls the main() routine it finds there. The compiled form of OtherClass is stored in OtherClass.class -- an entirely separate file. There's no indication at all that the source code for these two classes was once in the same file.

Now, if you're using an IDE of some kind (Eclipse, NetBeans, JBuilder, etc) then if you choose "Run" from a menu, it's possible that the IDE will misinterpret your wishes, and run the wrong class. It's slightly out of the ordinary to have two classes in the same source file which each contain a "main()", so perhaps there's even a bug in your IDE which is rarely observed.

Most (if not all) IDEs will have a "Run..." menu item, which opens a dialog box and lets you specify precisely what class to run. You might need to use this dialog.
 
Jeff Tian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right Ernest Friedman-Hill, it is to do with the IDE. The JVM executed the public class's main first when I used console instead.

I have never heard of this before. I was using Jcreator.

BTW, does having a public class and a default class in same file conceptually mean that the JVM should execute the public class's main first. Now, the more I think about it, the JVM doesn't really follow any rules in regard to class visibility.

Thanks for help guys, I was totally stumped.
[ March 29, 2005: Message edited by: Jeff Tian ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Tian:
BTW, does having a public class and a default class in same file conceptually mean that the JVM should execute the public class's main first. [ March 29, 2005: Message edited by: Jeff Tian ]


The key concept here is that the JVM does not read the .java code; it reads the .class files. If you take a look, you will see that 2 .class files were created from your 1 .java source: OtherClass.class and ClassTester.class. This way, there's no possibility for the JVM to be confused. (You might get confused, but it won't. )
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!