ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
First off !! Sorry to keep on bugging so many on such, im sure to be, tedious beginner questions.
Is it possible in java to define multiple classes in one source file?
If so how would i add it to something like this
Code:
//main class
class Project1
{
//Main method
public static void main( String[] arg)
{
//main stuff here
}
}
//would i be able to add another class here?
Its sufficient if you declare the driver class, the class which is going to be used for the compilation and execution as public.
Decalre the rest, as normal classes(They will have package access).
Code:
public class Driver
{
public static void main(String args[])
{
// ......
}
}
class Class1
{
Class1()
{
// ....
}
....
// no main()
}
class Class2
{
Class2()
{
// ....
}
// no main method
}
On compilation three classes Driver Class1$ Class2$ should be obtained.
I would suggest you to have the classes in separate files and have them all included in a package
Java deliberately only lets you put one public class in a source file - and the name of the source file has to be the name of the public class.
This might not necessarily seem like a good idea if you're working with small, "toy" programs - but it makes a LOT of sense once you're in a production environment with an application that has thousands of classes.
If you don't qualify access (i.e. if you don't say "public class" or "private class"), then the class has "package scope": any other class in the same source file, or in the same package, can see it. The only restriction is one (and only one) PUBLIC class per source file - you can have has many "private" or "default scope" classes as you want.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.