Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

multiple classes in same file?

843789Mar 5 2010 — edited Sep 15 2010
hey I saw some code on the internet where there were multiple classes in the same file and so I wrote my own version.
Could someone please tell me how this works? So the first class created has a "public" visibility but all the classes which are declared
after it cannot be declared either public, private or protected. I just don't know what the relationship is between each of these classes, and what it means to declare a class without any specified visiblity, coudd someone please explain this. i've noticed i can have ALL these classes specified without any visibility but if i change the first to "class multi" and make the second class public myClassOne, this causes a compile error.

Thanks
package MultiClassOneFile;

public class Multi {
	
	public static void main(String[] args){
		System.out.println(args[0]+" "+args[1]+" "+args[2]);
		
	}
}

class myClassOne {
	
	public static void main(String[] args){
		System.out.println(args[0]+" "+args[1]);
	}
	
	public void printOut(){
		System.out.println("This is myClassOne");
	}	
}

class myClassTwo {
	
	public static void main(String[] args){
		System.out.println(args[0]+" "+args[1]+" "+args[2]);
	}
	
	public void printOutAgain(){
		System.out.println("This is myClassTwo");
		
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 13 2010
Added on Mar 5 2010
7 comments
2,867 views