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

Answers were Sorted based on User's Feedback



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

Answer / ganesh slv

Sorry for the above..
its

If you declared a class as public, your file name should be
the same of your class name.

Is This Answer Correct ?    8 Yes 1 No

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

Answer / mahendra

In an aplication public class is main class with main()
method as entry point

java compiler allows only one public class in file

Is This Answer Correct ?    12 Yes 8 No

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

Answer / sadhi

In java we have 4 specifiers i.e:
1.public
2.private
3.protected
4.default
public: we can access these instance variables and instance
methods at out side the class and inside the class.
2.private:using this specifier only we can access with in
the class. there is no possibility to access the outside.
3.protected:we can access the class with in the package.
4.default:Is generally act as public when we not specify
anything specifier.

so,we can use public class any where that mean these
variables and methods accessed by another class that's why
there is no need to create more public classes in single file

Is This Answer Correct ?    8 Yes 4 No

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

Answer / ganesh slv

You can create one or more classes in single file. Only one
class may be declared as public. This is your Optional.

If you declared a class as public, your file name should be
the save of your class name.

Public class can be accessible from other applications.
Example, your applet code can be run in a web browser. This
public class may execute other classes in the same file.

If you declared two classes as public, your file name will
be what?

Note the Example :

class First {
// Some codes here
}

public class Second {
// some codes here
// In core, your main method should be here.
// You have to save the file as Second.
}

Is This Answer Correct ?    4 Yes 0 No

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

Answer / rakesh soni

Hi Mahendra,
This is OK that, compiler allowed only one class as public
per file. But my question is, why compiler allowed only one
class as a public in one file, why not more then one???

Is This Answer Correct ?    5 Yes 4 No

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

Answer / gopi

The class main method is start with public . This is
starting of the program, compiler have to know where to
start the program to compile. so only one public is there to
avoid confusion. We also use the public method to interact
with the out side of the programs. Without using of the
public class it can't possible to interact.

Is This Answer Correct ?    2 Yes 1 No

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

Answer / slvganesh.java@gmail.com

A file may have more than one class. Each class may or may
not have main method. But only one main () for a class.

Here's some example: Just Copy it and Run..

/**
* Save this file as Class1.java, bcoz i'm using a class
* Class1 which is public.
*/

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
}
}


Compile : javac Class1.java

After your compiled your can see three class files,
1. Class1.class
2. Class2.class
3. Class3.class

This is bcoz, java compiler converts the classes into files,
which has the bytecode instructions for that class.

Now you can execute the classes as you want.

Run : java Class1 (or)
java Class2 (or)
java Class3

After executing Class3 you may see the
Output :
This is from Class3..
This is from Class1..

Please understand, once you compiled your .java file, the
classes on that file will converted into .class (bytecodes).

Java interpreter looks only the .class.
In other words, java interpreter understand only the bytecodes.

Thanx for reading this..
Ganesh Slv.

Is This Answer Correct ?    1 Yes 0 No

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

Answer / malay

I think that you call only public classes from outside.Now
all these classe are kept in a .java file.So there should
be a one to one mapping between a public class
& .java/.class file.If there are more than one public
classes then JVM has to maintain a tracker that what all
classes are in single .java/.class file.Presently its easy
for JVM coz when a class is called it simply goes & load
the .class file with that name.

Is This Answer Correct ?    1 Yes 1 No

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

Answer / 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

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

Answer / janardhan

In an aplication public class is main class with main()
method as entry point

java compiler allows only one public class in file

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

How to sort array of 0 and 1 in java?

0 Answers  


Is java se open source?

0 Answers  


Explain the importance of finalize() method.

0 Answers  


Differences between C and Java?

0 Answers   TCS,


What is use of arraylist in java?

0 Answers  






What is an image buffer?

0 Answers  


What are peerless components in java programming?

0 Answers  


What do you mean by local class?

0 Answers  


what is the main class of all the classes

5 Answers   Photon,


what is Vector class?

2 Answers  


how to write a program for chat function using core java

1 Answers   Satyam,


What is java util collection?

0 Answers  


Categories