Why only one Class is public in one file? Explain in
details. Thanks in Advance.

Answer Posted / ganesh slv

/****** Program Starts Here ************/

/**
* Save this file as Class1.java. All should be in single
* file
*
* Date : 14.04.10
* @author Ganesh Slv
*/

public class Class1 { // Note this line

/* This Class is public. So your file name should be the
same name.
*/

public static void main (String arg[]) {
System.out.println (" This is from Class1.. ");
} // Main
} // Class1

class Class2 { // Note this line
/* This class is not public. so it doesn't matter
your file name.
*/

public static void main (String arg[]) {
System.out.println (" This from Class2..");
}
}

class Class3 {
/* This class Calls the main() in Class1.
*/

static public void main (String arg[]) {
System.out.println (" This is from Class3..");

// Creating obj to Class1
Class1 c1 = new Class1 ();
c1.main (arg); //Main must have one arg as String[] type
}
}

/********** Program Ends here ******/

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the default and parameterized constructors?

559


What is a default constraint?

618


What is number data type?

534


Why main() method is public, static and void in java ?

601


Why are the objects immutable in java?

550






Why is boolean important?

595


What do you mean by ternary operator in java?

562


What are different access specifiers in java?

571


Can we declare a class as static?

598


What is instance means in java?

566


Is array an object in java?

556


What is a byte array?

593


why Java does not support multiple inheritances?

706


What are the four versions of java?

552


Why bytecode is called bytecode?

595