answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

โˆ™ 12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

โˆ™ 12y ago

one

This answer is:
User Avatar

User Avatar

Wiki User

โˆ™ 11y ago

No.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Is it possible to have more than one public class in the same file?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why one public class in one source file in java?

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.


Is it possible for your Alice program to have more than one instance in a class?

It is possible to have more than one instance of the same class, because the class is simply the blue print for the actual object.


What is difference between partial Class and class?

If using 1 class per file as the standard, then it would be easier to understand that a partial class contains only a portion of the class definition in one file, while the other (may have more than one file) have the rest of the code belong to that class. One good example is the field definitions (data members) in one file, while the another file supply with behaviors that may not have anything to do with the data fields.


Why do file name and public class name always coincide in Java?

First of all, it only has to be the same when the class is public. And there is no explicit reason for that, it's just a convention that came along with old versions of java and people got used to it... They say it's because of the limited capabilities of the compiler to compile dependencies. When packages are stored in a file system (?7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true: * The type is referred to by code in other compilation units of the package in which the type is declared. * The type is declared public (and therefore is potentially accessible from code in other packages). This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory. When packages are stored in a database (?7.2.2), the host system must not impose such restrictions. In practice, many programmers choose to put each class or interface type in its own compilation unit, whether or not it is public or is referred to by code in other compilation units. It is not mandatory to say "file name equals to classname". > U can give your own name to your filename [ other than classname ] > at the time of compilation you just give your filename[other than classname] > After compilation you will get .class file with your class name.[classname.class] >.But at the time of loading ur program into JVM u just have to give the class name , This is possible even the main() is public/private. for eg:-consider have created a program in java with file name Ashish n class name is batra,now at the time of compilation u have to write "javac ashish.java" at the command prompt and at the same time the jvm create the .class object in the bin directory with filename =batra(batra.class) .Now at the time of running the program u have to write "java batra" at the command prompt. We say this statment that the file name should be same as the class name to make sure there is no confusion while compiling n running the program .Consider u have created many programs in java and now u want to run any one of them ,then it would be very difficult for u to recall the class name of that particular program .So to make it a simpler we offenly say that the class name should be same as the file name.


What is derived class in brief?

A derived class is any class that inherits from one or more other classes, known as base classes. The derived class inherits the sum total of all public and protected members of all its base classes, including their base classes. The derived class is a more specialised form of its base classes. Any members of the base classes that are declared virtual can be overridden, such that calling the base class method directly actually invokes the derived class method, thus enabling polymorphic behaviour.

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.


Why one public class in one source file in java?

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.


In Pokemon is it possible to have more than one file?

If by file you mean game then yes it is possible. Go to file-load game and chose an empty slot. HOpe this helps!


Is it possible for your Alice program to have more than one instance in a class?

It is possible to have more than one instance of the same class, because the class is simply the blue print for the actual object.


What is difference between partial Class and class?

If using 1 class per file as the standard, then it would be easier to understand that a partial class contains only a portion of the class definition in one file, while the other (may have more than one file) have the rest of the code belong to that class. One good example is the field definitions (data members) in one file, while the another file supply with behaviors that may not have anything to do with the data fields.


Why do file name and public class name always coincide in Java?

First of all, it only has to be the same when the class is public. And there is no explicit reason for that, it's just a convention that came along with old versions of java and people got used to it... They say it's because of the limited capabilities of the compiler to compile dependencies. When packages are stored in a file system (?7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true: * The type is referred to by code in other compilation units of the package in which the type is declared. * The type is declared public (and therefore is potentially accessible from code in other packages). This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory. When packages are stored in a database (?7.2.2), the host system must not impose such restrictions. In practice, many programmers choose to put each class or interface type in its own compilation unit, whether or not it is public or is referred to by code in other compilation units. It is not mandatory to say "file name equals to classname". > U can give your own name to your filename [ other than classname ] > at the time of compilation you just give your filename[other than classname] > After compilation you will get .class file with your class name.[classname.class] >.But at the time of loading ur program into JVM u just have to give the class name , This is possible even the main() is public/private. for eg:-consider have created a program in java with file name Ashish n class name is batra,now at the time of compilation u have to write "javac ashish.java" at the command prompt and at the same time the jvm create the .class object in the bin directory with filename =batra(batra.class) .Now at the time of running the program u have to write "java batra" at the command prompt. We say this statment that the file name should be same as the class name to make sure there is no confusion while compiling n running the program .Consider u have created many programs in java and now u want to run any one of them ,then it would be very difficult for u to recall the class name of that particular program .So to make it a simpler we offenly say that the class name should be same as the file name.


How can acting class help you?

It can help you be a better public speaker and be more sociable.


What is derived class in brief?

A derived class is any class that inherits from one or more other classes, known as base classes. The derived class inherits the sum total of all public and protected members of all its base classes, including their base classes. The derived class is a more specialised form of its base classes. Any members of the base classes that are declared virtual can be overridden, such that calling the base class method directly actually invokes the derived class method, thus enabling polymorphic behaviour.


What is filewriter?

It is a class that creates a file and allows you to write into the file. A better class for Java I/O is the PrintWriter class, which is more natural since its methods correspond to System.out, i.e. file.print("blah blah blah"), rather than remembering file.write();


What is single inheritance in c plus plus?

Multiple inheritance occurs when a class is derived directly from two or more base classes. class b1 {}; class b2 {}; class d: public b1, public b2 {}; // multiple inheritance class


Examples of inheritance in c plus plus?

Single-inheritance is where one class inherits directly from another class: class A {}; class B : public A {}; Here, class B inherits all the public and protected members of class A. Multiple-inheritance is where one class inherits directly from two or more classes: class A {}; class B {}; class C : public A, public B {}; Here, class C inherits all the public and protected members of both A and B. Multi-level inheritance is where one class inherits from another class that itself derived. class A {}; class B : public A {}; class C : public B {}; Here, class B inherits all the public and protected members of A while class C inherits all the public and protected members of B, including those inherited from A. Virtual inheritance applies to multi-level inheritance whereby a virtual base class becomes a direct ancestor to the most-derived class. This variation of inheritance is typically used in multiple inheritance situations where two or more intermediate classes inherit from the same base class: class A {}; class B : public virtual A {}; class C : public virtual A {}; class D : public B, public C {}; Here, classes B and C both inherit from class A. Without virtual inheritance this would mean class D would inherit two instances of A (B::A and C::A), thus creating ambiguity when referring to D::A. By employing virtual inheritance, D inherits directly from A, and both B and C inherit from D::A. In other words, B and C share the same instance of A. Another use of virtual inheritance is when you need to make a class final. class A; class B { friend class A; B() {} // private constructor }; class A : public virtual B { }; Here, class A is the final class. Class B is a helper class that has a private constructor while class A is declared a friend of class B. Class A is therefore the only class that can inherit from class B as it is the only class that can construct objects from class B. However, by inheriting class B virtually, we ensure that no other class can be derived from class A because virtual inheritance ensures that the most-derived class must be able to construct a class B object first. Currently, only class A has that privilege and must always be the most-derived class.


How friend function in c plus plus created?

Within a class declaration, include the following:friend ;For example:friend class MyClass; // friend classfriend void MyClass::SomeFunction(); // friend class method.friend void MyFunction(); // friend function.When declaring friend functions and friend class methods, you must declare an unambiguous prototype, including its return type and its parameter types, and all instances of the const keyword. If the function prototype is contained in another file, that file must also be included in the class declaration file.Friends can be declared anywhere in a class declaration. However, friends cannot be inherited so it doesn't matter if they're declared public, private or protected (access specifiers have no meaning to friends). But be aware that friends have privileged access to all members, including private and protected members. This is said to undermine the fundamentals of encapsulation, however the purpose of a friend is to extend the class interface, and is no more an undermining of encapsulation than is public inheritance. However, access should be limited to as few friends as possible; a class with many friends implies a fundamental design flaw in the class.