answersLogoWhite

0


Best Answer

The rules of Java state that the top-level main class needs to be declared "public." The idea behind this is probably because if it was not declared "public," then it would have to be either "private" or "protected." Since these other two types of classes can only be used by classes of the same package (or more local), and thus a non-public main class could not be called from the outside.

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

A file with .java extension when compiled. The Compiler looks for a unique class file that's declared public. A file cannot have more than one name Similarly in a java file there cannot be more than one public class that is used to uniquely identify the file name but if you try declaring two public class then the compiler throws an error.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

So that the class is accessible to all classes that you might code in the application. There are 4 basic access modifiers in java. They are:

1. Public

2. Protected

3. Default and

4. Private

Private is the most restrictive access modifier whereas public is the least restrictive.

If a variable or method is declared public, it means that it can be accessed by anyone or any other class (irrespective of which package they are in). Of course, it is mandatory that the class inside which the public method is placed is visible in the first place for the method or variable to be visible.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

A Class can be declared with the following statement:

public class AnandsFirstclass {}

Irrespective of the fact that this class does not have any code, this piece of code when saved in a file called AnandsFirstclass.java compiles just fine. Here public is an access modifier (we will see access modifiers in greater detail in one of the subsequent chapters), class is the keyword that is used to specify that a class is being declared and AnandsFirstclass is the name of the class we are creating.

There are many different types of classes that you can create. Some of which are:

1. Final Classes - A class that cannot be inherited (Dont worry about inheritance just yet. We will look into it in full detail in one of the later chapters)

2. Normal Classes - The type of class that we declared a few lines back

3. Abstract Classes - A class that is similar to a normal class but that does not provide full functional behaviour by itself. It has to be subclassed/inherited in order to be used.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

whenever we declare a private class only the members of class can access that function or that class , when ever we declare a class as public it can be access from any where , so it give challenges to security in java .. So we can use protected specifier so that the member of that class and the sub class can use that class....

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

Declaring an inner class as static tells Java to treat it like a top-level class instead of an inner class.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

To use the same class name as file name

Public is the access identifier . Before making a class we need to specify the class visibilty . Hence the precdence.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why one public class in one source file in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can a .java file contain more than one java classes?

Yes, it can. However, there can only be one public class per .java file, as public classes must have the same name as the source file.


Extension file for Java?

Java source files have the .java extension, compiled Java class files have the .class extension.


Any algo is used for converting class file to java source file?

You need a decompiler to convert class files to java source files. JAD is a Java Decompiler that can do it for you.


Is it possible to have more than one public class in the same file?

No. There can be multiple java classes in the same .java file, but the name of the file must match the name of the public class in the file.


Java program file must match the name of class with extension true or false?

The name of the .java file should exactly match with the name of the public class in the file. Ex: public class Test { ..... } this file should be saved as Test.java


What is java file in java?

A java file contains the code you write. One java file contains one class so for example when I want to make a class called Person, the source code is saved in Person.java


The name of a Java program file must match the name of the class with the extension java?

not exactly..... only If your class is public then the java program name should be the public class name with extension Sample.java >> public class Sample { public static void main(String[] args) { ..... } } NonPublicClass.java class SomeOtherName { ......... }


Why the name of a java file is same as the name of the public class?

It is not compulsory that the java file name and name of the public class should be same. if u will give java file name and public class name different then u have to compile and run the program with another names. for example: u have named class sample i.e. (public class sample) and main function is also defined in this class. and u have saved the file as abc.java then u will first comple the file as: javac abc.java now run the file (type java class name(in which main function is defined) i.e. java sample try it.


Which is the first non-comment statement in ajava program file?

It is most likely an "import Java. ...." statement. Which imports packages to be used in the source file. Or a class declaration "public class myClass { ".


How do you make a java class?

You write the source code, in a text editor, or better in a special IDE. The source code should have the extension ".java". You can have several classes in the same file. Then you compile the class to bytecode; this creates a file with extension ".class".


Files containing Java code must end with what extension?

They end with an extension .java Test.java would be a java source file. The contents of Test.java could be package xyz; import java.util.*; public class Test { ... ... .. . . }


What are the rules in naming a source file in JAVA program?

The basic rule is that the file name should match the name of the topmost public class in the .java file. The names are usually camel case and can contains alphabets and numbers. It should begin only with an alphabet.