aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Top Level Class doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Top Level Class doubt" Watch "Top Level Class doubt" New topic
Author

Top Level Class doubt

Sarang Khankhojr
Greenhorn

Joined: Jan 31, 2005
Posts: 17
What is exactly a Top level Class? As per my understanding
the class after which java file is named.
I tried this:
abstract class myclass
{
public void amethod();
public static void main(String[] args)
{
System.out.println("Just a test");
}
}
class1 extends myclass
{
public void amethod()
{
System.out.println("I am in subclass");
}
}
I named my file as myclass.java
Program compiles fine.But at runtime doesn't find main method?
Can someone explain why? And if that is true how can abstract class be top level class?
Sandra Marti
Ranch Hand

Joined: Jun 08, 2000
Posts: 63
sarang,
You have errors in your program. It should give you errors while compiling. Following is the corrected version:
abstract class myclass
{
public abstract void amethod();
public static void main(String[] args)
{
System.out.println("Just a test");
}
}
class class1 extends myclass
{
public void amethod()
{
System.out.println("I am in subclass");
}
}
The highlighted ones are the ones you had missed. The program runs fine with output 'just a test'.
A top-level class is a class that contains a nested class but is not itself a nested class.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Originally posted by sarang:
What is exactly a Top level Class? As per my understanding
the class after which java file is named.
I tried this:
abstract class myclass
{
public void amethod();
public static void main(String[] args)
{
System.out.println("Just a test");
}
}
class1 extends myclass
{
public void amethod()
{
System.out.println("I am in subclass");
}
}
I named my file as myclass.java
Program compiles fine.But at runtime doesn't find main method?
Can someone explain why? And if that is true how can abstract class be top level class?

Top Level class is any class which can be instanciated, it doesn't necessarily have to be the one that is the file name. You can have multiple classes defined in a single source file, however only one can be public. This also means that you can have multiple top level classes and multiple abstract classes.
Save the following in a file called myclass.java and compile it

then run class1 and class2 and see the results.
class1 and class2 are examples of a Top Level Class, you can create n object with these. myclass and myclass2 are not top level classes since they are absract and you can not create an object directly.

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Sarang,
the work of the JVM lies in trying to find the main method in applications and executing it. This main method executes without a instance of the class as it is static. As long as u do not do anything that is not allowed in the main method everything goes well. In the code if u had tried to instansitate an object of the abstract class then the compiler would be screaming. However since u have only printed out a statement to the console everything goes off well
Refer JLS 2 section 7.6 for Top level classes more details.

Regds
Rahul.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Top Level Class doubt