LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2006, 04:15 PM   #1
Fredstar
Member
 
Registered: Jul 2004
Location: Rochester, NY
Distribution: Fedora9::FreeBSD7.1
Posts: 296

Rep: Reputation: 30
Multiple Classes in one file?


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?
thanks
 
Old 02-28-2006, 04:45 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Public class name == file name. Java's 'feature'. You can add private classes, however.
 
Old 02-28-2006, 05:09 PM   #3
Fredstar
Member
 
Registered: Jul 2004
Location: Rochester, NY
Distribution: Fedora9::FreeBSD7.1
Posts: 296

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by Mara
Public class name == file name. Java's 'feature'. You can add private classes, however.
So hypothetically i could do the following
---FileName MyProject.java--

Class MyProject
{
public static void main( String[] args)
{
//some stuff here
}
}

private class PClass
{
//methods and all that good stuff
}

And then from the main MyProject method use the defined bellow classes by class.object() after creating instances in my main method?
 
Old 03-01-2006, 04:19 AM   #4
MRMadhav
Member
 
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167

Rep: Reputation: 30
Smile

I don't know if the method you proposed work, but actually i do it another way!

eg: filename = Project1.java

Code:
public class Project1
{
      public static void main(String[] args)
      {
          //some arguments here
      }

      class SubClass1
      {
        //methods of the subclass here
      }
}
however you should note that when the application is compiled it will create two files
1 - Project1.class
2 - Project1$SubClass1.class
 
Old 03-01-2006, 06:59 AM   #5
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Rep: Reputation: 15
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
 
Old 03-01-2006, 07:01 AM   #6
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Rep: Reputation: 15
What Mr. Madhav has created is I think is an inner class rather. It has its own usage
 
Old 03-01-2006, 11:18 AM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Fredstar -

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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
howto chowto change file permisions in multiplhange file permisions in multiple dirs? pingvina Linux - Software 3 01-13-2006 03:10 PM
scope of instantiation -> multiple classes purefan Programming 1 04-16-2005 05:51 AM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM
g++ multiple file compilation bluknight43 Programming 3 02-22-2005 07:26 PM
Compiling multiple Java Classes on demand dtheorem Programming 3 05-14-2004 12:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration