• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Multiple classes in one source code file

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
}
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you'r positive you saved your file as ClassTester.java?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. )
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic